Getting duplicate key values in array

Posted by WmasterJ on Stack Overflow See other posts from Stack Overflow or by WmasterJ
Published on 2010-03-22T21:35:18Z Indexed on 2010/03/22 21:41 UTC
Read the original article Hit count: 465

Filed under:
|

For some reason when trying to populate an array I am not getting the desired result. In the first part I create an array of arrays, the key of each has a name such as "Biology Education". But when I then populate that same array it doesn't for some reason use the same array element but a new one.

So part 1 results in an array with 13 array elements (empty). After part 2 has run the array has 26 array elements, with the first 13 empty and the other 13 populated as wanted.

The reason why I want to work is that the first 13 array elements are sorted. The last 13 are jumbled.

Why is this happening and how can I correct it?

// PART 1 
// Create array of research areas
$research_result = db_fetch_array($research['research_query_result']);
$research['areas'] = explode("\n", $research_result['options']);
// Put the values as key and every key the new value of an array object
$research['areas'] = array_fill_keys($research['areas'], array());

// PART 2 
foreach($research['user'] as $uid => &$user_object) {
    $user_object->profile_research_areas = explode(", ", $user_object->profile_research_areas);
    foreach($user_object->profile_research_areas as $key => $area) {
        $research['areas'][$area][] = $uid;
    }
}

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays