I want to make a function in php
which process the given array (of any dimension) and return all the value of that array. Here's my code so far:
<?php function resolve_array($args){ //To resolve 2 dimensional array foreach($args as $i => $i_val ){ if(is_array($i_val)){ foreach($i_val as $z=>$z_val ){ echo $z_val; } }else { echo $i_val; } }} ?>
this function resolves two dimensional arrays, i want to loop it to resolve the whole array.
I use this function to get the dimension of the array.
<?php function countdim($array){ //To get the dimension of array if (is_array(reset($array))) { $return = countdim(reset($array)) + 1; } else { $return = 1; } return $return; } ?>
I find myself lost in trying to display all the value,
any idea how to go ahead? Thank you!
I am not sure why you would want to do that.
Anyways, this code snippet collects all values in the multidimensional array into a separate array.
<?php
$values = array();
$collection = array(
array(
61854,
"sdkfjh",
"ldsjf" => array(
"25",
"jsdbf" => array(
9874158,
array(),
array(
6
)
)
),
),
23,
array(
"abcyx",
"hey",
12546,
"iii" => array(
"odsfgv",
521845
)
),
61874
);
function resolve($collection,&$values){
foreach($collection as $each_key => $each_value){
if(is_array($each_value)){
resolve($each_value,$values);
}else{
$values[] = $each_value;
}
}
}
resolve($collection,$values);
echo "<pre>";
print_r($values);
OUTPUT
Array
(
[0] => 61854
[1] => sdkfjh
[2] => 25
[3] => 9874158
[4] => 6
[5] => 23
[6] => abcyx
[7] => hey
[8] => 12546
[9] => odsfgv
[10] => 521845
[11] => 61874
)