0

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?

1 Answer 1

0

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.

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

1 Comment

Thanks mate... I've been dealing with this for days now. I'm progressing slowly but surely... Here I have another question that you can help me answer if you want: stackoverflow.com/questions/73581695/… . The final part includes joining all the header and all the detail in a table.

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.