Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get array of values from an associative arrays?

Tags:

php

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)
like image 693
Rachel Avatar asked Mar 18 '10 22:03

Rachel


People also ask

How do you find the data from an associative array?

Traversing the Associative Array: We can loop through the associative array in two ways. First by using for loop and secondly by using foreach. Example: Here array_keys() function is used to find indices names given to them and count() function is used to count number of indices in associative arrays.

Does In_array work for associative array?

in_array() function is utilized to determine if specific value exists in an array. It works fine for one dimensional numeric and associative arrays.

How do you print a multidimensional associative array?

Multidimensional associative array is often used to store data in group relation. Creation: We can create a multidimensional associative array by mapping an array containing a set of key and value pairs to the parent key. ); print_r( $languages );

What is array explain associative array with example?

An array refers to a data structure storing one or more related types of values in a single value. For instance, if you are looking to store 100 numbers, instead of specifying 100 variables, you can simply define an array of length 100.N.


1 Answers

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 ?)

like image 75
Pascal MARTIN Avatar answered Oct 12 '22 00:10

Pascal MARTIN