4

Here's my array:

Array
(
    [0] => Array
        (
            [same_key] => 1000
        )
    [1] => Array
        (
            [same_key] => 1001
        )
    [2] => Array
        (
            [same_key] => 1002
        )
    [3] => Array
        (
            [same_key] => 1003
        )
)

I'd like to get the following without using a foreach loop. Is this possible?

Array
(
    [0] => 1000
    [1] => 1001
    [2] => 1002
    [3] => 1003
)

Any tips?

Ryan
  • 14,682
  • 32
  • 106
  • 179
  • foreach loop is a good way .... why not foreach ?? – NullPoiиteя Sep 25 '12 at 18:09
  • It's been a while but I thought there were some array functions to do this without the expense of a loop. – Ryan Sep 25 '12 at 18:11
  • Check out this [answer][1] [1]: http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php maybe it will help you even more – faq Sep 25 '12 at 18:18
  • O yes. Sorry about the delayed acceptance of your answer. Worked like a charm. – Ryan Oct 03 '12 at 21:58

2 Answers2

4

The following will do the trick

$myArray = Array
(
    0 => Array
        (
            'adfadf'=> 1000
        ),
    1 => Array
        (
            'adfadf' => 1001
        ),
    2 => Array
        (
            'adfadf' => 1002
        ),
    3 => Array
        (
            'adfadf' => 1003
        )
);

$myArray = array_map('current', $myArray));
DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
2

you can do this by $array = array_map('current', $array);

live example

output

enter image description here

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • That code won't work in his case. array_map('current', $array[0]) will only return the first element in his array which for this case would be 1000. – DiverseAndRemote.com Sep 25 '12 at 18:18