0

I have a JSON item list in the database that holds the the serial number of different items to be printed according to the order of their serial number. The JSON structure is like this:

{
    "item_list": [
        {
            "item_sl": "1",
            "item_print": "flowers"
        },
        {
            "item_sl": "2",
            "item_print": "cars"
        }
    ]
}

After retrieving it from the database, I decoded the items in $items_array and then tried to create variables like $item1, $item2 ... which I wanted to assign to the item_print values from the JSON. The same print values have been already defined earlier ($cars, $flowers). Lastly, I wanted to print all of them. The code is:

$cars = 'Tesla is my favorite.';
$flowers = 'I love all flowers.';

$items_array = json_decode($items_data, true);
foreach ($items_array['item_list'] as $item_list) {
    foreach ($item_list['item_sl'] as $key => $n) {
        ${'item'.$n} = $item_list['item_print'][$key];
    }
}
$all_print = $item1.' '.$item2;
echo $all_print;

But $all_print is returning null, which tells me my approach is not correct. What am I missing here? What would be a better approach for printing the $cars and $flowers variables according to the serial numbers from the JSON?

3
  • you haven't assigned any value to the variables item1 and the item2 anywhere that's why all_print is empty !!
    – OMi Shah
    Sep 11, 2021 at 3:31
  • @OMiShah, Oh! But doesn't ${'item'.$n} = $item_list['item_print'][$key]; creates something like $item1 = $flowers?
    – Ashonko
    Sep 11, 2021 at 3:35
  • It does, but you're doing wrong.
    – OMi Shah
    Sep 11, 2021 at 3:54

2 Answers 2

1

First of all, there is no need to use multiple loops when you can achieve the same with a single loop.

Also, there is no need to create variables dynamically and assign values when you can directly decode the JSON and access each value without creating variables and assigning them dynamically.

This should work:

<?php

$items_data = '{
    "item_list": [
        {
            "item_sl": "1",
            "item_print": "flowers"
        },
        {
            "item_sl": "2",
            "item_print": "cars"
        }
    ]
}';

$items_array = json_decode($items_data, true);

foreach ($items_array['item_list'] as $item) {
    ${'item' . $item['item_sl']} = $item['item_print'];
}

$all_print = $item1.' '.$item2;
echo $all_print;

?>

Output:

flowers cars
1
  • Thanks for the answer but I want the output I love all flowers. Tesla is my favorite., that I assigned as $flowers, $cars earlier.
    – Ashonko
    Sep 11, 2021 at 4:20
0

For Your Code above, i don't Really Understand it well, but i think this may help if you are trying to loop items from a json encoded data.

$items_array = json_decode($items_data, true);

foreach ($items_array->item_list as $item_list) {
echo $item_list->item_sl;
echo $item_list->item_print;
}

EDITED Two Methods to achieve this

First:

$items_array = json_decode($items_data, true);

foreach ($items_array["item_list"] as $item_list) {
echo $item_list["item_sl"];
echo $item_list["item_print"].'<br>';
}

Second:

$items_array = json_decode($items_data);

foreach ($items_array->item_list as $item_list) {
echo $item_list->item_sl;
echo $item_list->item_print;
}

ADDITIONALLY:

if you want to display output based on the user item_print, then you can do this;

$items_array = json_decode($items_data, true);

foreach ($items_array["item_list"] as $item_list) {
  if ($item_list["item_print"] == 'cars') {
    echo $cars."<br>";
  }elseif($item_list["item_print"] == "flowers"){
    echo $flowers;
  }
}
2
  • 2
    You can't access property on an array! You see this line json_decode($items_data, true); it's decoding the JSON and converting to an array as true is passed for the second parameter.
    – OMi Shah
    Sep 11, 2021 at 3:54
  • my bad, Have Made some adjustments to the code and it should now be working fine. Sep 11, 2021 at 4:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.