amy burgess amy burgess - 2 months ago 6
PHP Question

how to make an all arrays (including associative array) to single level of array using php?

Hope you can help me with this. Because I'm trying to reorder them but i need first to make the be at a single level of array. From associative array to single array.

$MY_ASSOC_ARRAY
Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)

[1] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)

)
)

[1] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4
)
)
)
)
)
)


Alright now the array should not be in that multi level (associative) array instead it will look like this:

Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
)

[1] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
)

[2] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
)

[3] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)

[4] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)

[5] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4
)
)


I'm no sure how will that be possible in the most effecient way without using too much memory that will affect the speed with the use of Php Codeigniter. Thanks!

[UPDATE # 1]
here are the code that I have tried but the order is different

foreach($tree as $key => $value) {
$single[] = $value;
}


And this is the output for this failed attemp...

Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
)

[1] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
)

[2] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
)

[3] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4

)

[4] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)

[5] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)
)


The [NAME] => companion should be at the last array not on 4th ([3] => Array)

UPDATE # 2:

Feel bad about the down votes... if this question or problem is not useful on your end

Answer
<?php
    $array =  Array(
    0 => Array
        (
            'MAIN_ID' => 1,
            'ORDER' => 1,
            'NAME' => 'Animal',
            'PARENT_ID' => 0,
            'childs' => Array
                (
                    0 => Array
                        (
                            'MAIN_ID' => 4,
                            'ORDER' => 4,
                            'NAME' => 'doggie',
                            'PARENT_ID' => 1,
                            'childs' => Array
                                (
                                    0 => Array
                                        (
                                            'MAIN_ID' => 18,
                                            'ORDER' => 18,
                                            'NAME' => 'hunting',
                                            'PARENT_ID' => 4,
                                            'childs' => Array
                                                (
                                                    0 => Array
                                                        (
                                                            'MAIN_ID' => 21,
                                                            'ORDER' => 21,
                                                            'NAME' => 'setter',
                                                            'PARENT_ID' => 18,
                                                        ),

                                                    1 => Array
                                                        (
                                                            'MAIN_ID' => 22,
                                                            'ORDER' => 22,
                                                            'NAME' => 'pointer',
                                                            'PARENT_ID' => 18,
                                                        )

                                                )
                                        ),

                                    1 => Array
                                        (
                                            'MAIN_ID' => 19,
                                            'ORDER' => 19,
                                            'NAME' => 'companion',
                                            'PARENT_ID' => 4,
                                        )
                                )
                        )
                )
        )
);
$out = [];
$out = generateArray($array, $out);
print_r($out);

function generateArray($in, $out){
    foreach($in as $value){
        $childs = false;
        if(isset($value['childs'])){
            $childs = $value['childs'];
            unset($value['childs']);
        }
        $out[] = $value;
        if($childs)
            $out = generateArray($childs, $out);
    }
    return $out;
}
?>