0

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.

2
  • Can you provide a var_dump of $research['areas'] at the end of part 1, and a var dump of one of your $user_object->profile_research_areas variables? Commented Mar 22, 2010 at 22:02
  • The dump is to big but i think my last example in the main question should be enough. Commented Mar 22, 2010 at 22:39

2 Answers 2

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You've 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.

4 Comments

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.
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.
I use Krumo, similar to var_dump() and print_r(). That is what has helped me understand the different outputs that I have gotten.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.