-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

それは配列全体ですか?もしそうなら、単純な foreach() は高価ではないでしょうか?

foreach($array as $tuple)
{
  if($tuple['location'] == "FPER")
    $value = $tuple['maxdac'];
  break;
}
于 2013-10-24T11:35:50.863 に答える
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

少し異なるアプローチ。

<?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 に答える