Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP构造具有特定键的数组?_Php_Arrays - Fatal编程技术网

如何使用PHP构造具有特定键的数组?

如何使用PHP构造具有特定键的数组?,php,arrays,Php,Arrays,我有一个由4个元素组成的数组$device\u report=[]中包含这些数据 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" => 3

我有一个由4个元素组成的数组
$device\u report=[]中包含这些数据

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\u push()要求参数1为数组,如果给定空值


我只希望我的密钥是一个特定的设备Mac地址,而不是0,1,2,3

任何提示都将不胜感激

根据文件:

int-array\u推送(数组和$array,混合$value1[,混合$…])

array\u push()。数组的长度随着所推送的变量数量的增加而增加。具有与以下相同的效果:

在您的特定情况下,您试图创建一个新密钥并分配数组,因此您得到的错误是
$device\u report[$device\u mac]
不是数组。这确实是正确的,因为密钥不存在

要克服此问题,请直接分配数组,而不是使用
array\u push

试试这个:

$device_report[$device_mac] = $data;
而不是:

array_push($device_report[$device_mac], $data);
根据文件:

int-array\u推送(数组和$array,混合$value1[,混合$…])

array\u push()。数组的长度随着所推送的变量数量的增加而增加。具有与以下相同的效果:

在您的特定情况下,您试图创建一个新密钥并分配数组,因此您得到的错误是
$device\u report[$device\u mac]
不是数组。这确实是正确的,因为密钥不存在

要克服此问题,请直接分配数组,而不是使用
array\u push

试试这个:

$device_report[$device_mac] = $data;
而不是:

array_push($device_report[$device_mac], $data);
请尝试以下操作:

$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);
}
出现错误消息是因为
$device\u report[$device\u mac]
为空。必须使用数组初始化该值。使用以下代码,如果没有可用的数组,则使用空数组对其进行初始化:

//add this to init the array.
if (is_array($device_report[$device_mac]) === false) {
    $device_report[$device_mac] = [];
}
请尝试以下操作:

$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);
}
出现错误消息是因为
$device\u report[$device\u mac]
为空。必须使用数组初始化该值。使用以下代码,如果没有可用的数组,则使用空数组对其进行初始化:

//add this to init the array.
if (is_array($device_report[$device_mac]) === false) {
    $device_report[$device_mac] = [];
}

我不会用array\u push来做这个。没有理由

$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
}
$device_report=[];
foreach($devices作为$device){
$device\u mac=$device->device\u mac;//080027E2FC7D
$data=VSE::device($device\u mac);

$device\u report[$device\u mac]=$data;//我不会为此使用数组推送。没有理由这样做

$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
}
$device_report=[];
foreach($devices作为$device){
$device\u mac=$device->device\u mac;//080027E2FC7D
$data=VSE::device($device\u mac);

$device\u report[$device\u mac]=$data;//只要做:
$device\u report[$device\u mac]=$data;
简短优雅。谢谢。另请参阅:只要做:
$device\u report[$device\u mac]=$data;
简短优雅。谢谢。另请参阅: