Mil Pro
Mil Pro

Reputation: 13

Array ksort only shows non-like values?

Have an array for a ranking script. Some times the key will be the same. They are numeric. When the sort is ran, only non-like values are echoed. Can't figure out the fix.

$list = array( $value1 => 'text', $value2 => 'text', $value3 => 'text');

krsort($list);
foreach ($list as $key => $frame) {
    echo $frame;
}

Upvotes: 1

Views: 936

Answers (2)

Hauke P.
Hauke P.

Reputation: 2833

Going by what you wrote in the comments to this question and my other answer, I'd recommend to switch keys and values.

<?php 

$list = array( "frame1" => 4, "frame2" => 2, "frame3" => 99, "frame4" => 42 );

arsort($list);
foreach ($list as $frame => $ranking) {
    echo $frame;
}

?>

Upvotes: 0

Hauke P.
Hauke P.

Reputation: 2833

If you assign two values to the same key in an array, the first value will be overridden by the second. You'll therefore end up with only one value for that key in the array.

To resolve this, I'd suggest to change your array structure like this:

<?php 

$list = array( $key1 => array($key1member1, $key2member2),
               $key2 => array($key2member1),
               $key3 => array($key3member1, $key3member2, $key3member3) );

krsort($list);
foreach ($list as $key => $frames) {
    foreach ($frames => $frame) {
        echo $frame;
    }
}

?>

Upvotes: 2

Related Questions