Jorgy tb
Jorgy tb

Reputation: 9

Printing a multidimensional array

How do I print this array?

<?php
    $datos = array(
        array('AZUAY', array('P', 'Q'))
    );
    //print array
    foreach ($datos as $dato1) {
        foreach ($dato1 as $v2) {
            echo $v2;
            foreach ($v2 as $v3)
                echo $v3;
        }
        echo "<br>";
    }
?>

Upvotes: 0

Views: 26

Answers (2)

angelcool.net
angelcool.net

Reputation: 2546

Something like this should work:

$datos = array(
    array('AZUAY', array('P', 'Q'))
);

function printWeirdArray($array)
{
    foreach($array as $i)
    {
       if(is_array($i))
       {
           printWeirdArray($i);
       }
       else
       {
           print $i."-";
       }
    }
}

printWeirdArray($datos);

The above code outputs:

AZUAY-P-Q-

Good luck!!

Upvotes: 0

Jonathan Lam
Jonathan Lam

Reputation: 17351

You can use printf() or var_dump() as a simple pretty-printer:

print_r($datos);
var_dump($datos);

Output:

print_r()

Array ( [0] => Array ( [0] => AZUAY [1] => Array ( [0] => P [1] => Q ) ) )

var_dump()

array(1) { [0]=> array(2) { [0]=> string(5) "AZUAY" [1]=> array(2) { [0]=> string(1) "P" [1]=> string(1) "Q" } } }

Upvotes: 2

Related Questions