简体   繁体   中英

Add key/value on each array item - PHP

I am trying to do a simple thing by adding an extra key/value for each items in my array. I am having difficulties with this because the extra key/value is added at the bottom and not inside each array(key).

This is my Array:

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

What i need is for each product add an increment number (image_number), i am doing it like this below:

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

But the end result is this:

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

What i need/want is this result:

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

You don't need 2 foreach() loop, with a single foreach() you can append image_number easily.

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

WORKING DEMO: https://3v4l.org/T30B1

Try:

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

You have an array that looks like this:

array(
    0 => [
        0 => {
            "id": 11
        }, 
        1 => {
            "id": 13
        }
    ]
]

Essentially, the above is the setup of your multi-dimensional array, I've included the keys so you can see it clearer.

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++;

    }

}

So in order to access the actual data, we need to do the following:

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

You were trying to set information into:

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

Which is why it was being set in the root array, and not the array within the array.

There are ways to modify the array using the key but I would use a reference & to the exposed values in the foreach :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM