-1

我有这个名为 $result 的数组

Array ( [0] => Array ( [location] => DNM Room (N) [maxdac] => 12.0 [mindac] => 2.0 [avgdac] => 7.00000 ) [1] => Array ( [location] => DNM Room (S) [maxdac] => 1.0 [mindac] => 1.0 [avgdac] => 1.00000 ) [2] => Array ( [location] => Feeder Cabinet (N) [maxdac] => 20.0 [mindac] => 20.0 [avgdac] => 20.00000 ) [3] => Array ( [location] => Feeder Cabinet (S) [maxdac] => 10.0 [mindac] => 10.0 [avgdac] => 10.00000 ) [4] => Array ( [location] => FM Vault (N) [maxdac] => 10.0 [mindac] => 10.0 [avgdac] => 10.00000 ) [5] => Array ( [location] => FM Vault (S) [maxdac] => 5.0 [mindac] => 5.0 [avgdac] => 5.00000 ) [6] => Array ( [location] => FMLCTA [maxdac] => 1.0 [mindac] => 1.0 [avgdac] => 1.00000 ) [7] => Array ( [location] => FMSA (N) [maxdac] => 2.0 [mindac] => 2.0 [avgdac] => 2.00000 ) [8] => Array ( [location] => FMSA (S) [maxdac] => 0.1 [mindac] => 0.1 [avgdac] => 0.10000 ) [9] => Array ( [location] => FPER [maxdac] => 3.0 [mindac] => 2.0 [avgdac] => 2.25000 ) [10] => Array ( [location] => FT Room (N) [maxdac] => 0.1 [mindac] => 0.1 [avgdac] => 0.10000 ) [11] => Array ( [location] => Moderator Room [maxdac] => 20.0 [mindac] => 7.0 [avgdac] => 14.25000 ) [12] => Array ( [location] => PAER [maxdac] => 2.0 [mindac] => 1.0 [avgdac] => 1.57500 ) [13] => Array ( [location] => Passage [maxdac] => 3.0 [mindac] => 1.0 [avgdac] => 1.62500 ) [14] => Array ( [location] => Pump Room [maxdac] => 35.0 [mindac] => 20.0 [avgdac] => 27.50000 ) [15] => Array ( [location] => RAB [maxdac] => 1.0 [mindac] => 1.0 [avgdac] => 1.00000 ) [16] => Array ( [location] => RB Sump [maxdac] => 0.1 [mindac] => 0.1 [avgdac] => 0.10000 ) [17] => Array ( [location] => SFSB [maxdac] => 0.0 [mindac] => 0.0 [avgdac] => 0.00000 ) [18] => Array ( [location] => West Passage [maxdac] => 3.0 [mindac] => 0.5 [avgdac] => 1.42500 ) )

我想在上面的数组中搜索 [location]='FPER' 并获取键 [maxdac] 的值。尝试了几个选项,没有任何效果。不熟悉数组。任何方法

选中此选项

foreach ($array as $key => $value) {
if($value['location']=='FPER'){echo $value['maxdac'];}
}

我得到值 3.0

是否可以在函数中使用上述循环并获取不同位置的值?

例如

function getstat($location, $param){

foreach ($array as $key => $value) {
    if($value['location']==$location){return $value[$param];}
    }
}


$myvalue=getstat('FPER', 'maxdac');

echo $myvalue;

上述功能不起作用。似乎 $location 和 $param 在 foreach 中是未知的。

任何建议请

4

3 回答 3

0

我自己解决了

function getstat($location, $param){
global $array;

foreach ($array as $key => $value) {

if($value['location']==$location){return $value[$param];}
}

}


echo getstat('FPER', 'maxdac');

结果为 3.0

于 2013-10-25T07:14:05.387 回答
0

那是整个数组吗?如果是这样,一个简单的 foreach() 不会很贵吗?

foreach($array as $tuple)
{
  if($tuple['location'] == "FPER")
    $value = $tuple['maxdac'];
  break;
}
于 2013-10-24T11:35:50.863 回答
0

略有不同的方法。

<?php
$array = array(
    array('location' => 'FPER', 'maxdec' => rand(1, 10)),
    array('location' => 'ABCD', 'maxdac' => rand(1, 10)),
    array('location' => 'FPER', 'maxdec' => rand(1, 10)),
    array('location' => 'ABCD', 'maxdac' => rand(1, 10)),
    array('location' => 'FPER', 'maxdec' => rand(1, 10)),
    array('location' => 'ABCD', 'maxdac' => rand(1, 10)),
);

$filter = function($item) {
    return 'FPER' === $item['location'];
};

foreach (array_filter($array, $filter) as $key => $item) {
    printf("(%d) - %s\n", $key, $item['maxdec']);
}
于 2013-10-24T11:42:51.653 回答