[英]How can I construct an array with a specific key using PHP?
I have an array of 4 elements $device_report = [];
我有4个元素的数组$device_report = [];
with these data in it 这些数据都在里面
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
]
]
Right now, I have this 现在,我有这个
$device_report = [];
foreach ($devices as $device){
$device_mac = $device->device_mac; //080027E2FC7D
$data = VSE::device($device_mac);
array_push($device_report,$data);
}
I've tried 我试过了
$device_report = [];
foreach ($devices as $device){
$device_mac = $device->device_mac; //080027E2FC7D
$data = VSE::device($device_mac);
array_push($device_report[$device_mac],$data);
}
It give me error : 它给我错误:
array_push() expects parameter 1 to be array, null given
I just want to my key to be a specific device Mac Address rather than 0,1,2,3. 我只想将密钥设置为特定的设备Mac地址,而不是0、1、2、3。
Any hints will be much appreciated ! 任何提示将不胜感激!
Per the documentation, array_push
: 根据文档array_push
:
int array_push ( array &$array , mixed $value1 [, mixed $... ] )
array_push()
treats array as a stack, and pushes the passed variables onto the end of array.array_push()
将数组视为堆栈,并将传递的变量压入数组的末尾。 The length of array increases by the number of variables pushed. 数组的长度增加了所推送变量的数量。 Has the same effect as: 具有与以下相同的效果:
In your particular case, you're trying to create a new key and assign the array, so you're getting the error that $device_report[$device_mac]
is not an array. 在您的特定情况下,您尝试创建一个新键并分配该数组,因此您将收到错误消息$device_report[$device_mac]
不是数组。 This is indeed correct since the key doesn't already exist. 这确实是正确的,因为密钥尚不存在。
To overcome this, assign the array directly as oppose to using array_push
. 为了克服这个问题,与使用array_push
相反,直接分配数组。
Try this: 尝试这个:
$device_report[$device_mac] = $data;
instead of: 代替:
array_push($device_report[$device_mac], $data);
Try the following: 请尝试以下操作:
$device_report = [];
foreach ($devices as $device){
$device_mac = $device->device_mac; //080027E2FC7D
$data = VSE::device($device_mac);
//add this to init the array.
if (is_array($device_report[$device_mac]) === false) {
$device_report[$device_mac] = [];
}
array_push($device_report[$device_mac],$data);
}
The error message appear because $device_report[$device_mac]
is null. 由于$device_report[$device_mac]
为空,出现错误消息。 You have to initialize the value with an array. 您必须使用数组初始化值。 With the following code you initialize it with a empty array if no array is available: 如果没有可用的数组,则使用以下代码将其初始化为空数组:
//add this to init the array.
if (is_array($device_report[$device_mac]) === false) {
$device_report[$device_mac] = [];
}
I wouldnt use array_push for this. 我不会为此使用array_push。 No reason to. 没有理由
$device_report = [];
foreach ($devices as $device){
$device_mac = $device->device_mac; //080027E2FC7D
$data = VSE::device($device_mac);
$device_report[$device_mac]=$data; // <-- This line changed
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.