tags:

views:

107

answers:

2

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;
    }
}

An example of the end result is that 2 element within the array $research['areas'] looks like this:

...(26 elements)
$research['areas']['Biology Education'] (0 elements)
$research['areas']['Biology Education'] (11 elements)
...

Hope it is clear.

+1  A: 

You could put some debugging output in there, using var_dump or print_r, to see what's in the array at each stage. Beyond that, I'm not sure what your question is. Which element is being duplicated? The only place I can see something like that happening is here:

    $research['areas'][$area][] = $uid;

Where the [] portion means "append $uid to $research['areas'][$area] as a new element".


update:

so you're got something like this after part 1:

$research = array(
   'areas' => array(
        'Biology education' => array(),
        'Chemistry education' => array()
   )
);

you want something like this to occur in part 2:

$research = array(
   'areas' => array(
        'Biology education' => array(42, 3.14159265, 2.181281),
        'Chemistry education' => array()
   )
);

but are ending up with:

$research = array(
   'areas' => array(
        'Biology education' => array(),
        'Biology education' => array(42, 3.14159265, 2.181281), // <--duplicate entry?
        'Chemistry education' => array()
   )
);

or am I guessing totally wrong? If that's the case, then most likely your two "Biology Education" keys are actually not the same. Perhaps one has an unprintable character embedded in it somewhere (trailing null? an &nbsp; you're not seeing in a web output, etc...). Something will be causing them to be different keys, otherwise your PHP has a serious glitch in it somewhere.

Marc B
So I have a key in the array called "Biology Education". And the $area variable that you pointed out has the string "Biology Education" aswell. But instead of appending the $uid variable to the existing element it creates a new one. Resulting in duplicates with keys of same names.
WmasterJ
We need to see a dump of the array you're ending up with as well as a mocked-up dump of the array you *wish* you were ending up with.
webbiedave
I use Krumo, similar to var_dump() and print_r(). That is what has helped me understand the different outputs that I have gotten.
WmasterJ
Sorry but that didn't work either. I'm not able to find the problem still and will simply try another approach on the key things I am trying to solve with my code. Thanks!
WmasterJ
+1  A: 

Can't see why it would make a difference but I usually confuse my self with & and foreach loops, so try this and see it makes a difference:

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

// PART 3
foreach($research['user'] as $uid => $user_object) {
    foreach($user_object->profile_research_areas as $key => $area) {
        $research['areas'][$area][] = $uid;
    }
}
Martin