合并PHP数组的值最高

时间:2011-08-30 23:45:46

标签: php arrays

我有这三个子数组,我想制作一个数组 在每个数组中搜索最高值,并放入其键 进入另一个数组,创建一个新的键/键值数组

修改 我正在查看所有0,然后全部1,然后全部2来找到那里的最高值。我想总是从所有时间最高数字开始,然后在键变为不可用时继续前进到最小

输入就是

Array
(
    Array
    (
        [0] => 16 
        [1] => 27 
        [2] => 36 // highest, 2 is now unavailable
    )
    Array
    (
        [0] => 27
        [1] => 13.5  // highest, 1 is now unavailable
        [2] => 9 
    )
    Array
    (
        [0] => 81 // highest, 0 is now unavailable
        [1] => 18
        [2] => 27 
    )
)

输出

Array
(
    [0] => [2]
    [1] => [1]
    [2] => [0]
)

2 个答案:

答案 0 :(得分:2)

如果我找对你的话,沿着这些方向应该做的事情:

$result = array();

foreach ($array as $numbers) {
    $highestKey = null;
    $highestNum = null;

    foreach ($numbers as $key => $value) {
        if ($value > $highestNum && !in_array($key, $result)) {
            list($highestKey, $highestNum) = array($key, $value);
        }
    }

    $result[] = $highestKey;
}

答案 1 :(得分:0)

$results = array();
foreach ($array as $value) {
    arsort($value);
    $temp = array();        
    foreach ($value as $subkey => $subval) {
        if (!in_array($subkey, $results))
        $temp[] = $subkey;
    }       
    $results[] = $temp[0];
}   

http://codepad.org/PAVHni6k