打印多维数组

时间:2015-11-07 00:51:30

标签: php arrays multidimensional-array formatting

如何打印此阵列?

<?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>";
    }
?>

2 个答案:

答案 0 :(得分:2)

您可以将printf()var_dump()用作简单的漂亮打印机:

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

输出:

<强> 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" } } }

答案 1 :(得分:0)

这样的事情应该有效:

$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);

以上代码输出:

AZUAY-P-Q-

祝你好运!!