Hello what is the easiest way to change this array to 1D array, i can do that using for loop or foreach, but i'm curious to check if there is an easier way. THANKS

Array
(
    [0] => Array
        (
            [id] => 1
        )

    [1] => Array
        (
            [id] => 2
        )

    [2] => Array
        (
            [id] => 3
        )
)

Comments

What should the resulting array look like?

Written by Flavius Stef

Accepted Answer

$output_ar = array_map('array_shift', $input_ar);

The array_shift() function grabs the first key/val pair out of an array and returns the value, so applying it to each of the arrays in your top-level array, and combining the results, will result in a 1-d list of the id's.

If your arrays actually have more info than just the id field in them, then you'd probably want to define a function that specifically pulls out whatever field(s) you want and returns those, and then use that function with array_map.

Written by Amber
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki