sun sun - 3 months ago 12
PHP Question

how can i get multidimensional array values using foreach?

I want to use a multidimensional array in different functions.so i am making it as a global variable(array).i created a multidimensional array and made it as global to access in different function.now how can i get the values from it using foreach loop?
here is my code

$test=array(
array(
"input1"=>"v1",
"input2"=>"v2"),
array(
"input3"=>"v3",
"input4"=>"v4")
);

class testing
{
function testp()
{
global $test;
foreach($test as $key => $value)
{
echo $value;
}
var_dump($test);
echo is_array($test);
}
}

$obj = new testing();
$obj->testp();


i used is_array and
var_dump
to confirm whether its an array.
all are fine
and its shwoing Error suppression ignored. now how can i get the values from it?

Answer

It is array of arrays, what works for top order array, works further as well:

foreach($test as $key => $value)
{
   foreach($value as $k => $v){
      echo $v;
   }
}

This will echo your values v1, v2, v3, v4 one after another.

Comments