Skip to content
Advertisement

array_input is the array which can be dynamic up to any level, I want to convert input array into the array_output format using php

Input Array : Input array is like below and level in it can be dynamic up to any level,

JavaScript

OUTPUT ARRAY: Output array should be like below, I tried using 2 foreach loop and accessing previous Level and compare it to next Level. But it is creating problem where Level is more than 2 times.

JavaScript

This is the code i tried but it’s not giving result what i want:

JavaScript

Advertisement

Answer

You may give this a try. See comments for step-by-step explanation. Outputs:

JavaScript

Code:

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