1

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

2 回答 2

2

如果将两个值分配给数组中的同一个键,则第一个值将被第二个值覆盖。因此,您最终会得到数组中该键的一个值。

为了解决这个问题,我建议像这样改变你的数组结构:

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

?>
于 2013-06-01T17:19:11.937 回答
0

根据您在对该问题的评论和我的其他答案中所写的内容,我建议您切换键和值。

<?php 

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

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

?>
于 2013-06-01T18:39:09.663 回答