Skip to content
Advertisement

how to convert $request format in laravel

I search around for a bit but found nothing that did exactly what I’m after nor anything that really aided me in achieving what I’d like

I have data in this format:

JavaScript

I’d like to convert it all to this format:

JavaScript

i tried:

JavaScript

but get no success

Advertisement

Answer

You got it the other way around. You need to loop on the rows first (0 and 1), then in each column (name, phone, email, cnic), loop each value:

JavaScript

Or using array_combine + array_keys + array_column would work as well:

JavaScript

Sample Output

Sidenote: If this came from a form, I think this is the classic case of <input name="name[]" in your html form.

If you follow this kind of structure:

JavaScript

You would not need to do such array transposition in PHP. Once you submit the form, you already got yourself your expected array structure above. But that’s just an assumption on my part.

If it doesn’t come from a form, feel free to disregard this sidenote.

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