Search code examples
phpmysqlmultidimensional-arrayimplode

Escape the top level of a multi dimensional array


Hi I can't seem to find an answer anywhere to this question I have. I know its something simple.

Lets say you have a two dimensional array it looks something like this:

[0] => Array
            (
                [0] => Array
                    (
                        [taxonomy] => dairy
                        [terms] => cheese
                        [field] => slug
                    )

                [1] => Array
                    (
                        [taxonomy] => dairy
                        [terms] => yogurt
                        [field] => slug
                    )

                [2] => Array
                    (
                        [taxonomy] => brand
                        [terms] => independent
                        [field] => slug
                    )

            )

how do I break it down into a single dimensional array like so

                [0] => Array
                    (
                        [taxonomy] => dairy
                        [terms] => cheese
                        [field] => slug
                    )

                [1] => Array
                    (
                        [taxonomy] => dairy
                        [terms] => yogurt
                        [field] => slug
                    )

                [2] => Array
                    (
                        [taxonomy] => brand
                        [terms] => independent
                        [field] => slug
                    )"

The reason I ask this because I wish to make dynamic query, that needs to be formatted as such. Normally I would just foreach the original array and cycle though it and generate the needed information, but the information is part of larger array. Which means I cannot use a foreach loop from inside the larger array.

Does anyone have any ideas on how I could escape the parent array reach such formatting?


Solution

  • So I do not think you are looking for a recursive implode.

    I think the function better suited for your needs is var_export.

    <?php
    $string = var_export($array, true);