Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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,我试图做一件简单的事情,为数组中的每个项添加一个额外的键/值。我对此有困难,因为额外的键/值是在底部添加的,而不是在每个数组(键)中添加的 这是我的数组: [ [{ "id": 11, "product_id": 3, "sku": 30000011, "name": "BCAA 2:1:1 400g Fruit Punch", "slug": "bcaa-211-400g-fruit-punch", "files_id": 1397, "

我试图做一件简单的事情,为数组中的每个项添加一个额外的键/值。我对此有困难,因为额外的键/值是在底部添加的,而不是在每个数组(键)中添加的

这是我的数组:

[
[{
    "id": 11,
    "product_id": 3,
    "sku": 30000011,
    "name": "BCAA 2:1:1 400g Fruit Punch",
    "slug": "bcaa-211-400g-fruit-punch",
    "files_id": 1397,
    "image_name": "bcaa-211-400g-proteinfabrikken-1.jpg",
    "image_uuid": "494bacb0-13ae-11e7-b439-adf9395810da",
    "image_size": 295472,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-494bacb0-13ae-11e7-b439-adf9395810da\/bcaa-211-400g-proteinfabrikken-1.jpg"
}, {
    "id": 13,
    "product_id": 3,
    "sku": 30000013,
    "name": "BCAA 2:1:1 400g Lemon-Lime",
    "slug": "bcaa-211-400g-lemon-lime",
    "files_id": 1399,
    "image_name": "bcaa-211-400g-proteinfabrikken-3.jpg",
    "image_uuid": "496a1420-13ae-11e7-ba90-ddc728050acd",
    "image_size": 294101,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-496a1420-13ae-11e7-ba90-ddc728050acd\/bcaa-211-400g-proteinfabrikken-3.jpg"
}]
]
[{
    "0": {
        "id": 11,
        "product_id": 3,
        "sku": 30000011,
        "name": "BCAA 2:1:1 400g Fruit Punch",
        "slug": "bcaa-211-400g-fruit-punch",
        "files_id": 1397,
        "image_name": "bcaa-211-400g-proteinfabrikken-1.jpg",
        "image_uuid": "494bacb0-13ae-11e7-b439-adf9395810da",
        "image_size": 295472,
        "image_type": "image\/jpeg",
        "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-494bacb0-13ae-11e7-b439-adf9395810da\/bcaa-211-400g-proteinfabrikken-1.jpg"
    },
    "1": {
        "id": 13,
        "product_id": 3,
        "sku": 30000013,
        "name": "BCAA 2:1:1 400g Lemon-Lime",
        "slug": "bcaa-211-400g-lemon-lime",
        "files_id": 1399,
        "image_name": "bcaa-211-400g-proteinfabrikken-3.jpg",
        "image_uuid": "496a1420-13ae-11e7-ba90-ddc728050acd",
        "image_size": 294101,
        "image_type": "image\/jpeg",
        "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-496a1420-13ae-11e7-ba90-ddc728050acd\/bcaa-211-400g-proteinfabrikken-3.jpg"
    },
    "image_number": 1
}]
[
[{
    "id": 11,
    "product_id": 3,
    "sku": 30000011,
    "name": "BCAA 2:1:1 400g Fruit Punch",
    "slug": "bcaa-211-400g-fruit-punch",
    "files_id": 1397,
    "image_name": "bcaa-211-400g-proteinfabrikken-1.jpg",
    "image_uuid": "494bacb0-13ae-11e7-b439-adf9395810da",
    "image_size": 295472,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-494bacb0-13ae-11e7-b439-adf9395810da\/bcaa-211-400g-proteinfabrikken-1.jpg"
    "image_number": 0 <--- This
}, {
    "id": 13,
    "product_id": 3,
    "sku": 30000013,
    "name": "BCAA 2:1:1 400g Lemon-Lime",
    "slug": "bcaa-211-400g-lemon-lime",
    "files_id": 1399,
    "image_name": "bcaa-211-400g-proteinfabrikken-3.jpg",
    "image_uuid": "496a1420-13ae-11e7-ba90-ddc728050acd",
    "image_size": 294101,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-496a1420-13ae-11e7-ba90-ddc728050acd\/bcaa-211-400g-proteinfabrikken-3.jpg"
    "image_number": 1 <--- This
}]
]
我需要的是为每个产品添加一个增量编号(图像编号),我是这样做的:

$i = 0;
        foreach($product_variants as $key => $value){
            foreach($value as $keys => $values){
                $product_variants[$key]['image_number'] = $i++;
            }
        }
但最终结果是:

[
[{
    "id": 11,
    "product_id": 3,
    "sku": 30000011,
    "name": "BCAA 2:1:1 400g Fruit Punch",
    "slug": "bcaa-211-400g-fruit-punch",
    "files_id": 1397,
    "image_name": "bcaa-211-400g-proteinfabrikken-1.jpg",
    "image_uuid": "494bacb0-13ae-11e7-b439-adf9395810da",
    "image_size": 295472,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-494bacb0-13ae-11e7-b439-adf9395810da\/bcaa-211-400g-proteinfabrikken-1.jpg"
}, {
    "id": 13,
    "product_id": 3,
    "sku": 30000013,
    "name": "BCAA 2:1:1 400g Lemon-Lime",
    "slug": "bcaa-211-400g-lemon-lime",
    "files_id": 1399,
    "image_name": "bcaa-211-400g-proteinfabrikken-3.jpg",
    "image_uuid": "496a1420-13ae-11e7-ba90-ddc728050acd",
    "image_size": 294101,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-496a1420-13ae-11e7-ba90-ddc728050acd\/bcaa-211-400g-proteinfabrikken-3.jpg"
}]
]
[{
    "0": {
        "id": 11,
        "product_id": 3,
        "sku": 30000011,
        "name": "BCAA 2:1:1 400g Fruit Punch",
        "slug": "bcaa-211-400g-fruit-punch",
        "files_id": 1397,
        "image_name": "bcaa-211-400g-proteinfabrikken-1.jpg",
        "image_uuid": "494bacb0-13ae-11e7-b439-adf9395810da",
        "image_size": 295472,
        "image_type": "image\/jpeg",
        "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-494bacb0-13ae-11e7-b439-adf9395810da\/bcaa-211-400g-proteinfabrikken-1.jpg"
    },
    "1": {
        "id": 13,
        "product_id": 3,
        "sku": 30000013,
        "name": "BCAA 2:1:1 400g Lemon-Lime",
        "slug": "bcaa-211-400g-lemon-lime",
        "files_id": 1399,
        "image_name": "bcaa-211-400g-proteinfabrikken-3.jpg",
        "image_uuid": "496a1420-13ae-11e7-ba90-ddc728050acd",
        "image_size": 294101,
        "image_type": "image\/jpeg",
        "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-496a1420-13ae-11e7-ba90-ddc728050acd\/bcaa-211-400g-proteinfabrikken-3.jpg"
    },
    "image_number": 1
}]
[
[{
    "id": 11,
    "product_id": 3,
    "sku": 30000011,
    "name": "BCAA 2:1:1 400g Fruit Punch",
    "slug": "bcaa-211-400g-fruit-punch",
    "files_id": 1397,
    "image_name": "bcaa-211-400g-proteinfabrikken-1.jpg",
    "image_uuid": "494bacb0-13ae-11e7-b439-adf9395810da",
    "image_size": 295472,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-494bacb0-13ae-11e7-b439-adf9395810da\/bcaa-211-400g-proteinfabrikken-1.jpg"
    "image_number": 0 <--- This
}, {
    "id": 13,
    "product_id": 3,
    "sku": 30000013,
    "name": "BCAA 2:1:1 400g Lemon-Lime",
    "slug": "bcaa-211-400g-lemon-lime",
    "files_id": 1399,
    "image_name": "bcaa-211-400g-proteinfabrikken-3.jpg",
    "image_uuid": "496a1420-13ae-11e7-ba90-ddc728050acd",
    "image_size": 294101,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-496a1420-13ae-11e7-ba90-ddc728050acd\/bcaa-211-400g-proteinfabrikken-3.jpg"
    "image_number": 1 <--- This
}]
]
我需要/想要的是这个结果:

[
[{
    "id": 11,
    "product_id": 3,
    "sku": 30000011,
    "name": "BCAA 2:1:1 400g Fruit Punch",
    "slug": "bcaa-211-400g-fruit-punch",
    "files_id": 1397,
    "image_name": "bcaa-211-400g-proteinfabrikken-1.jpg",
    "image_uuid": "494bacb0-13ae-11e7-b439-adf9395810da",
    "image_size": 295472,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-494bacb0-13ae-11e7-b439-adf9395810da\/bcaa-211-400g-proteinfabrikken-1.jpg"
}, {
    "id": 13,
    "product_id": 3,
    "sku": 30000013,
    "name": "BCAA 2:1:1 400g Lemon-Lime",
    "slug": "bcaa-211-400g-lemon-lime",
    "files_id": 1399,
    "image_name": "bcaa-211-400g-proteinfabrikken-3.jpg",
    "image_uuid": "496a1420-13ae-11e7-ba90-ddc728050acd",
    "image_size": 294101,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-496a1420-13ae-11e7-ba90-ddc728050acd\/bcaa-211-400g-proteinfabrikken-3.jpg"
}]
]
[{
    "0": {
        "id": 11,
        "product_id": 3,
        "sku": 30000011,
        "name": "BCAA 2:1:1 400g Fruit Punch",
        "slug": "bcaa-211-400g-fruit-punch",
        "files_id": 1397,
        "image_name": "bcaa-211-400g-proteinfabrikken-1.jpg",
        "image_uuid": "494bacb0-13ae-11e7-b439-adf9395810da",
        "image_size": 295472,
        "image_type": "image\/jpeg",
        "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-494bacb0-13ae-11e7-b439-adf9395810da\/bcaa-211-400g-proteinfabrikken-1.jpg"
    },
    "1": {
        "id": 13,
        "product_id": 3,
        "sku": 30000013,
        "name": "BCAA 2:1:1 400g Lemon-Lime",
        "slug": "bcaa-211-400g-lemon-lime",
        "files_id": 1399,
        "image_name": "bcaa-211-400g-proteinfabrikken-3.jpg",
        "image_uuid": "496a1420-13ae-11e7-ba90-ddc728050acd",
        "image_size": 294101,
        "image_type": "image\/jpeg",
        "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-496a1420-13ae-11e7-ba90-ddc728050acd\/bcaa-211-400g-proteinfabrikken-3.jpg"
    },
    "image_number": 1
}]
[
[{
    "id": 11,
    "product_id": 3,
    "sku": 30000011,
    "name": "BCAA 2:1:1 400g Fruit Punch",
    "slug": "bcaa-211-400g-fruit-punch",
    "files_id": 1397,
    "image_name": "bcaa-211-400g-proteinfabrikken-1.jpg",
    "image_uuid": "494bacb0-13ae-11e7-b439-adf9395810da",
    "image_size": 295472,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-494bacb0-13ae-11e7-b439-adf9395810da\/bcaa-211-400g-proteinfabrikken-1.jpg"
    "image_number": 0 <--- This
}, {
    "id": 13,
    "product_id": 3,
    "sku": 30000013,
    "name": "BCAA 2:1:1 400g Lemon-Lime",
    "slug": "bcaa-211-400g-lemon-lime",
    "files_id": 1399,
    "image_name": "bcaa-211-400g-proteinfabrikken-3.jpg",
    "image_uuid": "496a1420-13ae-11e7-ba90-ddc728050acd",
    "image_size": 294101,
    "image_type": "image\/jpeg",
    "image_url": "https:\/\/d2m146bkiftioz.cloudfront.net\/7-496a1420-13ae-11e7-ba90-ddc728050acd\/bcaa-211-400g-proteinfabrikken-3.jpg"
    "image_number": 1 <--- This
}]
]
[
[{
“id”:11,
“产品标识”:3,
“sku”:3000011,
“名称”:“BCAA 2:1:1 400g水果潘趣酒”,
“slug”:“bcaa-211-400g-FROUT-punch”,
“文件id”:1397,
“图像名称”:“bcaa-211-400g-proteinfabrikken-1.jpg”,
“图像”;“494bacb0-13ae-11e7-b439-adf9395810da”,
“图像大小”:295472,
“图像类型”:“图像\/jpeg”,
“图片url:“https:\/\/d2m146bkiftioz.cloudfront.net\/7-494bacb0-13ae-11e7-b439-adf9395810da\/bcaa-211-400g-proteinfabrikken-1.jpg”
“图像编号”:0尝试:

您有一个如下所示的数组:

array(
    0 => [
        0 => {
            "id": 11
        }, 
        1 => {
            "id": 13
        }
    ]
]
基本上,上面是多维数组的设置,我已经包括了键,以便您可以更清楚地看到它

foreach($product_variants as $key => $value) {

    // On the first iteration, $key is equal to 0, and $value is the next array.

    foreach($value as $keys => $values) {

        // On the first iteration, $keys is equal to 0, and $values is the data in the array.

        $product_variants[$key]['image_number'] = $i++;

    }

}
因此,为了访问实际数据,我们需要执行以下操作:

$product_variants[0][0]['data'] = 'value;
您试图将信息设置为:

$product_variants[0]['data'] = 'value;
这就是为什么它被设置在根数组中,而不是数组中的数组中。

请尝试:

您有一个如下所示的数组:

array(
    0 => [
        0 => {
            "id": 11
        }, 
        1 => {
            "id": 13
        }
    ]
]
基本上,上面是多维数组的设置,我已经包括了键,以便您可以更清楚地看到它

foreach($product_variants as $key => $value) {

    // On the first iteration, $key is equal to 0, and $value is the next array.

    foreach($value as $keys => $values) {

        // On the first iteration, $keys is equal to 0, and $values is the data in the array.

        $product_variants[$key]['image_number'] = $i++;

    }

}
因此,为了访问实际数据,我们需要执行以下操作:

$product_variants[0][0]['data'] = 'value;
您试图将信息设置为:

$product_variants[0]['data'] = 'value;

这就是为什么它被设置在根数组中,而不是数组中的数组。

有一些方法可以使用键修改数组,但我会使用引用
&
来引用
foreach
中的公开值:

$i = 0;
foreach($product_variants as &$value){
    foreach($value as &$values){
        $values['image_number'] = $i++;
    }
}

有几种方法可以使用键修改数组,但我将使用
引用
foreach
中的公开值:

$i = 0;
foreach($product_variants as &$value){
    foreach($value as &$values){
        $values['image_number'] = $i++;
    }
}

您不需要2个
foreach()
循环,只需一个
foreach()
就可以轻松地追加图像编号

$array =json_decode($json,1)[0];
foreach($array as $key=>$value){
    $array[$key]['image_number'] = $key;
}
print_r($array);

工作演示:

您不需要2个
foreach()
循环,只需一个
foreach()
就可以轻松添加图像编号

$array =json_decode($json,1)[0];
foreach($array as $key=>$value){
    $array[$key]['image_number'] = $key;
}
print_r($array);

工作演示:

谢谢这项工作!!当我有两个foreach语句时,为什么我需要[key]和[key]。谢谢这项工作!!为什么我需要[key]和[key]当我有两个foreach语句时。变量数组在数组中有什么原因吗?没有@Keydose我删除了它,我的坏!API返回了一个数组,我在另一个错误的移动中保存了它。现在修复了:)谢谢有什么原因变量数组在数组中吗?没有@Keydose我删除了它,我的坏!API返回了一个数组,我是在另一个错误的移动中被保存。现在修复:)谢谢
foreach
引用会导致副作用。使用
$index=>$value
并更改循环中的索引值。
foreach
引用会导致副作用。使用
$index=>$value
并更改循环中的索引值。