-1

I want to convert this array in a single dimensional flat array without losing the sort order.

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => Computer
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [title] => keyboard
                            [parent_id] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                            [title] => Mouse
                                            [parent_id] => 4
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 7
                                                            [title] => webcam
                                                            [parent_id] => 6
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 43
            [title] => Mobile
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [title] => bar phones
                            [parent_id] => 43
                        )

                    [1] => Array
                        (
                            [id] => 47
                            [title] => Touchscreen
                            [parent_id] => 43
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 41
                                            [title] => Samsung
                                            [parent_id] => 47
                                        )

                                    [1] => Array
                                        (
                                            [id] => 44
                                            [title] => Micromax
                                            [parent_id] => 47
                                        )

                                    [2] => Array
                                        (
                                            [id] => 45
                                            [title] => Huawei
                                            [parent_id] => 47
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 46
            [title] => Camera
            [parent_id] => 0
        )

    [3] => Array
        (
            [id] => 42
            [title] => Heater
            [parent_id] => 0
        )

)

1 Answers1

0

Give it try with below function:

function makeOneDimensionArray(array $array, &$res = array())
{
    foreach($array as $arr)
    {
        $res[] = array(
            'id' => $arr['id'],
            'title' => $arr['title'],
            'parent_id' => $arr['parent_id']
        );
        if(isset($arr['children']))
        {
            makeOneDimensionArray($arr['children'], $res);
        }
    }
    return $res;
}

$finalArr = makeOneDimensionArray($your_array);
print_r($finalArr);
Hardik Solanki
  • 3,153
  • 1
  • 17
  • 28