开发者

Getting duplicate key values in array

开发者 https://www.devze.com 2022-12-24 06:51 出处:网络
For some reason when trying to populate an array I am not getting the desired result. In the first part I create an ar开发者_高级运维ray of arrays, the key of each has a name such as \"Biology Educati

For some reason when trying to populate an array I am not getting the desired result. In the first part I create an ar开发者_高级运维ray 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号