我有4個元素$device_report = [];
這些數據在它如何使用PHP構建具有特定鍵的數組?
array:4 [▼
0 => array:2 [▼
"up_bytes" => 2818
"down_bytes" => 948
]
1 => array:2 [▼
"up_bytes" => 472
"down_bytes" => 439
]
2 => array:2 [▼
"up_bytes" => 3364
"down_bytes" => 1317
]
3 => array:2 [▼
"up_bytes" => 3102
"down_bytes" => 1682
]
]
數組現在,我有這個
$device_report = [];
foreach ($devices as $device){
$device_mac = $device->device_mac; //080027E2FC7D
$data = VSE::device($device_mac);
array_push($device_report,$data);
}
我試過
$device_report = [];
foreach ($devices as $device){
$device_mac = $device->device_mac; //080027E2FC7D
$data = VSE::device($device_mac);
array_push($device_report[$device_mac],$data);
}
它給我的錯誤:
array_push() expects parameter 1 to be array, null given
我只是想我的鑰匙是一個特定的設備的MAC地址,而不是0,1,2,3。
任何提示將不勝感激!
只要做:'$ device_report [$ device_mac] = $ data;' – Rizier123
短小精緻。謝謝。 – ihue
另請參閱:http://stackoverflow.com/q/2121548/3933332 – Rizier123