Trying to display multidimentional array using php construct does not work at all:
<?php
$_arr=array("Names"=>array("mola","dola","kola","hola"));
echo $_arr;
?>
but if ,we use print_r() function than it displays the whole array in a specific format why?
Try the foreach
Statement
foreach($_arr as $key => $value)
{
foreach($value as $v)
echo "$key => $v<br>
";
}
First of all there is a parenthesis missmatch, the closing parenthesis is missing at the end of the array.
echo $_arr;
should just return "array". print_r($_arr)
"Prints human-readable information about a variable " see http://www.php.net/manual/en/function.print-r.php
You Asked why..because this is the behaviour of this function? ;)
You can either use print_r
or var_dump
to print your array. Otherwise, use echo
in a foreach
loop to iterate over the array. Another foreach
inside the first one if you are iterating over a 2D array. And so on...
function print_array($array){
if(!is_array($array)){
return false;
}
foreach($array as $v){
if(is_array($v)){
print_array($v);
}
else{
echo "$v <br>
";
}
}
}
print_array(array("one", array("a", "b", "c")));
Output:
one
a
b
c