得到foreach $ key并存入差异变量

时间:2015-05-13 09:51:11

标签: php

$Ascore = 30
$Bscore = 30
$Cscore = 20
$Dscore = 20     


 $data = array(
                'A1' => $Ascore,
                'B1' => $Bscore,
                'C1' => $Cscore,
                'D1' => $Dscore
                );

$highest = max($data);


 foreach($data as $key => $value){
    if($value === $highest){
    echo $key;
    //echo output (t1,t3);
    }
像这样的事情 让它们存储在不同的变量中

$type1 = $key[0]; //this will be t1//
$type2 = $key[1]; //this will be t3//

我的目的是以某种方式制作我在$key找到的元素并将它们放入不同的变量中,我将如何实现这一目标?我有这个想法,但我无法继续工作。

2 个答案:

答案 0 :(得分:0)

你的意思是存储相应值是数组中最大值的键吗?如果是这样,试试:

.*Connection reset.*?\b([^ .]+\.xls)\b

如果您必须将密钥放在单独的变量中,只需添加:

$highest = max($data);
$max_keys = array();

foreach($data as $key => $value){
    if ($value === $highest){
        array_push($max_keys, $key);
    }
}

答案 1 :(得分:0)

假设我正确地阅读了这个问题,因为它有点模糊:

$data = [1,3,5,3,5];

$highest = max($data);
$result = array_keys(
    array_filter(
        $data,
        function($value) use ($highest) {
            return $value == $highest;
        }
    )
);
var_dump($result);