Bas Bas - 4 months ago 11
PHP Question

Getting array values of inner array keys

I have an array like this:

array(2) {
[0]=>
array(1) {
[0]=>
object(Bas\WWW\Router\Route\URIs\URI\Parameters\Parameter\Parameter)#13 (2) {
["name":"Bas\WWW\Router\Route\URIs\URI\Parameters\Parameter\Parameter":private]=>
string(2) "name1"
["value":"Bas\WWW\Router\Route\URIs\URI\Parameters\Parameter\Parameter":private]=>
string(2) "30"
}
}
[1]=>
array(2) {
[0]=>
object(Bas\WWW\Router\Route\URIs\URI\Parameters\Parameter\Parameter)#12 (2) {
["name":"Bas\WWW\Router\Route\URIs\URI\Parameters\Parameter\Parameter":private]=>
string(2) "name2"
["value":"Bas\WWW\Router\Route\URIs\URI\Parameters\Parameter\Parameter":private]=>
string(6) "289213"
}
[1]=>
object(Bas\WWW\Router\Route\URIs\URI\Parameters\Parameter\Parameter)#15 (2) {
["name":"Bas\WWW\Router\Route\URIs\URI\Parameters\Parameter\Parameter":private]=>
string(6) "name3"
["value":"Bas\WWW\Router\Route\URIs\URI\Parameters\Parameter\Parameter":private]=>
string(5) "00123"
}
}
}


And I want to retrieve all objects in it, which are the values of the inner array keys.

My approach was by looping the first outer values, then looping the inner values and placing those in a seperate array.

$a = [];
foreach ($parameters as $parameter) {
foreach ($parameter as $data) {
$a[] = $data;
}
}


Is there a better way for doing this instead of looping both of the arrays?

Answer

Simple solution is merge your sub_arrays, treating your array as a list of arguments to array_merge:

$a = call_user_func_array('array_merge', $parameters);
Comments