Skip to content
Advertisement

Organizing array Iteration generated from nested loops PHP

I have a loop that fetches data from an excel file using phpspreadsheet. data fetching has no problem, but what I want to do is organize the data to a multi-dimensional array then convert it to JSON so I can access it through javascript.

here’s the code:

JavaScript

when I run this code, it shows this text:

JavaScript

Now I want to organize this to an array that looks like this:

JavaScript

Is it achievable? thanks in advance.

Advertisement

Answer

You need to push data with specified, first-level keys in your foreach() loop. Don’t bother giving a key to the grouping value (phone type), just use that value as the grouping key.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement