在每个数组项目上添加键/值-PHP

时间:2019-07-18 15:40:11

标签: php arrays

我试图通过为数组中的每个项目添加一个额外的键/值来做一件简单的事情。我遇到了麻烦,因为额外的键/值是在底部而不是在每个array(key)内添加的。

这是我的数组:

[
[{
    "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"
}]
]

我需要为每个产品添加一个增量编号(image_number),如下所示:

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

但是最终结果是这样的:

[{
    "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
}]
]

3 个答案:

答案 0 :(得分:1)

您不需要2个foreach()循环,只需一个foreach(),您就可以轻松附加 image_number

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

工作演示: https://3v4l.org/T30B1

答案 1 :(得分:0)

尝试:

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

您有一个看起来像这样的数组:

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;

这就是为什么要在根数组中而不是在数组中设置数组的原因。

答案 2 :(得分:0)

有些方法可以使用键来修改数组,但是我会使用for item in (data['data']) if item['un'] == tecNo: print(tabulate([[item['fud'], item['un'], str(item['lsn']), str(item['fns']), str(item['musage']), str(item['hu']), str(item['mu']), str(item['hb']), str(item['mb'])]],headers=head, tablefmt="grid"))``` [1]: https://i.stack.imgur.com/ZHODf.png [2]: https://i.stack.imgur.com/pNci2.png 的引用&

foreach