0

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
}]
]
2
  • 1
    Is there any reason why the array of variants is within an array? Commented Jul 18, 2019 at 15:48
  • No @Keydose i removed it, my bad! the API returned an Array and i saved in another, bad move. Fixed it now :) Thanks Commented Jul 18, 2019 at 15:54

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

foreach with reference can cause side effects. Use $index =>$value and change the indexed value within the loop.
0

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.

1 Comment

Thanks this worked!! Why do i need [key] and [keys] when i have two foreach statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.