6

How can I get an array of values from an associative array ?

Associate array Example:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )
    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )
    [2] => Array
        (
            [0] => 7
        )
)

Desired Output

Array
(1,2,3,4,5,6,7)

4 Answers 4

8

Not sure it'll suit you, as it's PHP >= 5.3 only, but here's a possible solution, using array_walk_recursive and a closure (see Anonymous functions) :

$array = array(
    array(1, 2, 3), 
    array(4, 5, 6), 
    array(7), 
);

$result = array();
array_walk_recursive($array, function ($value, $key) use (& $result) {
    $result[] = $value;
});
var_dump($result);

And the result :

array
  0 => int 1
  1 => int 2
  2 => int 3
  3 => int 4
  4 => int 5
  5 => int 6
  6 => int 7

Basicaly, the closure is the only way I got this to work : it's used to import the $result variable, by reference, into the anonymous function.



And, just to post it, the only I got this working for PHP 5.2 (i.e. not using a closure) is with this :

$result = array();
array_walk_recursive($array, 'my_func', & $result);
var_dump($result);

function my_func($value, $key, & $result) {
    $result[] = $value;
}

Which works too -- but raises a warning :

Deprecated: Call-time pass-by-reference has been deprecated

Unfortunatly, I didn't find a way of getting this to work without passing $result by reference at call-time :-(
(Maybe someone else has an idea, about how to do that ?)

Sign up to request clarification or add additional context in comments.

2 Comments

+1. Although I have an answer that doesn't use closures, this was my first thought of how to do it. Answering PHP questions will be more fun when PHP5.3 becomes more common.
@Yacoby : PHP 5.3 FTW ! (anonymous functions and closures are one thing I would really like to be able to use, at work... But we're using PHP 5.2 ;-( )
1

Do an array_merge() on each separate member.

$test = Array
(
    "0" => Array
        (
            0 => 1,
            1 => 2,
            2 => 3,
        ),
    "1" => Array
        (
            0 => 4,
            1 => 5,
            2 => 6,
        ),
    "2" => Array
        (
            0 => 7,
        )
);

print_r(array_merge($test[0],$test[1],$test[2]));
// Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) 

Comments

1

under php 5.3 this should be really nice and convenient, because the array elements used by array_reduce are interpreted as mixed rather than fixed as integer

so

print_r(array_reduce($test, 'array_merge'))

I haven't tested it -- no copy of 5.3 -- at least in theory that should do it. Under <5.3 you'll get errors.

Note that this only merges the top level of array, unlike the solution using array_walk_recursive.

1 Comment

+1 for noticing the recent change to array_reduce. But, you need to supply an inital array to start with. array_reduce($aNestedArray, 'array_merge', array())
1
$flattened = call_user_func_array('array_merge', $array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.