Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Dimensional array value print without any key

I want to print a array where I don't know the dimension of the array value. Now the fact is when I use echo '<pre>' and then print_r($array) it will show the Key and value with <br> display. But I want to display only the value and not the key of the array. That $array may contain multi-dimensional array value or single value or both.

like image 951
Nirjhar Mandal Avatar asked Aug 25 '14 07:08

Nirjhar Mandal


2 Answers

You have to use recursive function:

$array_list = array('a',array(array('b','c','d'),'e')); // Your unknown array
print_array($array_list);
function print_array($array_list){
    foreach($array_list as $item){
        if(is_array($item)){
            print_array($item);
        }else{
            echo $item.'<br>';
        }
    }
}
like image 116
Md Riad Hossain Avatar answered Nov 14 '22 14:11

Md Riad Hossain


try this Recursive function

function print_array($array, $space="")
{
    foreach($array as $key=>$val)
    {
        if(is_array($val))
        {
            $space_new = $space." ";
            print_array($val, $space_new);
        }   
        else
        {
            echo $space." ".$val." ".PHP_EOL;
        }
    }
}

See Demo

like image 22
Satish Sharma Avatar answered Nov 14 '22 14:11

Satish Sharma