Home » Php » php – Multidimensional Array merged to single line array

php – Multidimensional Array merged to single line array

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am working with an array that sometimes has a multidimensional array. I am trying to accomplish making this array into a single line array.

Array
(
    [0] => Array
        (
            [attribute_code] => 203
            [attribute_value] => Array
                (
                    [0] => 24214
                )

        )

    [1] => Array
        (
            [attribute_code] => 252
            [attribute_value] => Array
                (
                    [0] => 22865
                    [1] => 25086
                )

        )

)

This is what I am trying to accomplish

Array
(
    [0] => {"attribute_code":"203","attribute_value":"24214"}
    [1] => {"attribute_code":"252","attribute_value":["22865","25086"]}
)

What would the best way to accomplish this?

How to&Answers:

That is just JSON encoding each element:

$result = array_map("json_encode", $array);