我有兩個數組,一個是「OperID」,另一個是「OperSums」。所述OperID數組包含ID號,並且OperSums數組包含附連到總它看起來像這樣的ID:訪問PHP中關聯數組時未定義的變量
陣列1
{[0] => oper1 [1] => oper2 [2] => 3 [3] => oper4 [4] => oper5 [5] => oper6 [6] => oper7 [7] => oper8 [8] => oper9}
陣列2
{["oper3"]=> float(17498.5) ["oper1"]=> float(10383.5) ["oper2"]=> float(6277) ["oper4"]=> float(10224.67) ["oper6"]=> float(3955.65) ["oper5"]=> float(4997.78) ["oper8"]=> float(11382) ["oper9"]=> float(5072.1) ["oper7"]=> float(14759) ["oper-nb3n0hah-1tueubqo"]=> float(1033.45) ["oper-50f6e4ad-9effbec7"]=> float(3058) ["oper-4f05a90b-03b379f9"]=> float(12112.5) ["oper-4db82d0b-796a3081"]=> float(621) ["oper-qxr9ryex-bsmm0g6f"]=> string(4) "0.00" ["oper-qtgjvw8y-1uqtw058"]=> float(10023) ["oper-487b885e-dbbae536"]=> string(6) "340.00" ["oper-shcuaee2-yldfdxsd"]=> float(467) ["oper-416fd551-da6937eb"]=> float(6563) ["oper-50564d75-f1da98ec"]=> string(4) "0.00" ["oper-l65tf5ex-w5qfinca"]=> float(1746) ["oper-52657816-3d6516e2"]=> float(3495) ["oper-4a82c3be-bccc185d"]=> float(0) ["oper-1f2mnwry-nfywuasi"]=> string(6) "255.95"}
我想要收集只有在兩個數組中的運算符值,並顯示數組2中每個浮點數內每個運算符的總數,並截斷所有額外數據。我想截斷所有額外數據的原因是因爲我將在圖中顯示信息。對於關聯數組我真的不怎麼樣,所以任何指向正確方向的指針或提示都將不勝感激。順便說一下,這在PHP中。
這是我與迄今使用的代碼:
$operSums = array();
$operearnedArray[] = array(
'amount' => $row['ChargeAmount'],
'id' => $row['OperatorID']);
foreach ($operearnedArray as $value) {
if($value['id'] == '' || $value['id'] == null) {
continue;
}
if(array_key_exists($value['id'], $operSums)) {
$operSums[$value['id']] += $value['amount'];
} else {
$operSums[$value['id']] = $value['amount'];
}
}
foreach ($OperSums as $id => $value) {
if (in_array($id,$OperID)) {
echo $id.' => '.$value;
}
}
但它拋出這些錯誤消息:
注意:未定義的變量:OperSums 警告:的foreach提供了無效的參數()
這些錯誤信息的發生是由於第二foreach循環僅
可能的重複[顯示一個關聯數組中的值,其鍵值作爲另一個數組中的值存在](http://stackoverflow.com/questions/26664907/display-values-from-one-聯想陣列 - 其中的鍵存在作爲價值在一個) – MrWhite 2014-10-30 23:15:31