Home > OS >  How to iterate a JSON ARRAY with internal Array in Blade?
How to iterate a JSON ARRAY with internal Array in Blade?

Time:09-03

I am working with MongoDB and so far I have been able to loop through the following JSON correctly with the ability to manipulate the data to display.

controller

$json = '[{
"T": {
   "HD": {
       "HD01": "1",
       "HD06": "20201006033942",
       "HD08": "3736803"
       }
   }
},
{
"T": {
   "HD": {
       "HD01": "1",
       "HD06": "20201006035419",
       "HD08": "3736803"
       }
   }
}]'

$keysToRemove = ['HD01', 'HD06'];

$data = collect(json_decode($data, true))
       ->flatten(2)
       ->map(function ($array, $key) use ($keysToRemove) {
           foreach ($keysToRemove as $key) {
               if (array_key_exists($key, $array)) {
                  unset($array[$key]);
               }
           }
       return $array;
     });

return view('dashboard.book.index', [
    'data' => $data
]);

Blade

<table>
    <tr>
        @foreach ($data->first() as $key => $value)
        <th>{{ $key }}</th>
        @endforeach
    </tr>
    @foreach ($data as $items)
    <tr>
        @foreach ($items as $key => $value)
        <td>{{ $value }}</td>
        @endforeach
    </tr>
    @endforeach
</table>

result

Table

Now, now I must go through another one that has an array inside it, and which is made up as follows:

controller

$data = '[{
        "T": {
            "SIEL" : {
                    "SIE" : [
                        {
                            "sequenceNumber" : 0,
                            "itemId" : "3010",
                            "itemDescription" : "BAN VACCINE COSTELLET",
                            "unitAmount" : 29950,
                            "quantity" : 1,405,
                            "subTotalAmount" : 42080,
                            "discountAmount" : 0,
                            "totalAmount" : 42080
                           
                        },
                        {
                            "sequenceNumber" : 1,
                            "itemId" : "1010",
                            "itemDescription" : "CROLETA COOKIE",
                            "unitAmount" : 4800,
                            "quantity" : 0.605,
                            "subTotalAmount" : 2904,
                            "discountAmount" : 0,
                            "totalAmount" : 2904
                        }
                    ]
                }
            }
            
        }]';

How do I go through it following the logic of the first one, to obtain the 'SIE' data?

CodePudding user response:

It is a similar principle to your previous question. Decode your JSON to an associative array, convert to a collection, flatten it and work with the result.

$sieData = collect(json_decode($data, true))->flatten(3)->collapse();

return view('view.blade.php', compact('sieData'));

The value of sieData is an array of associated arrays. You already have how to loop over such a structure in the example code you provided in your question.

  • Related