Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 打印阵列、子阵列等_Php_Arrays_Recursion - Fatal编程技术网

Php 打印阵列、子阵列等

Php 打印阵列、子阵列等,php,arrays,recursion,Php,Arrays,Recursion,我有一个多维数组,其中包含一系列类别。对于这个例子,我用服装类别填充了它 $categories = array( 'Fashion' => array( 'Shirts' => array( 'Sleeve' => array( 'Short sleeve', 'Long sleeve' ), 'Fit' =>

我有一个多维
数组
,其中包含一系列类别。对于这个例子,我用服装类别填充了它

$categories = array(
    'Fashion' => array(
        'Shirts' => array(
            'Sleeve' => array(
                'Short sleeve',
                'Long sleeve'
            ),
            'Fit' => array(
                'Slim fit',
                'Regular fit'
            ),
            'Blouse'
        ),
        'Jeans' => array(
            'Super skinny',
            'Slim',
            'Straight cut',
            'Loose',
            'Boot cut / flare'
        )
    ),
);
我希望能够像这样打印整个
数组

--Fashion
----Shirts
-------Sleeve
---------Short sleeve
---------Long sleeve
-------Fit
---------Slim fit
---------Regular fit
----Blouse
我想我需要使用某种递归函数


我如何才能做到这一点?

递归函数将为您提供答案:

function printAll($a) {
    if (!is_array($a)) {
        echo $a, ' ';
        return;
    }

    foreach($a as $k => $value) {
             printAll($k);
             printAll($value);

    }
}
试试这个

<?php 
function print_r_recursive($array){
    if(!is_array($array)){
        echo $array;
        return; }
    foreach ($array as  $value) {
        if(is_array($value)){
            print_r_recursive($value);
        }
    }
}
 ?>

我尝试使用您给定的数组并获得以下结果:

$categories = array(
    'Fashion' => array(
        'Shirts' => array(
            'Sleeve' => array(
                'Short sleeve',
                'Long sleeve'
            ),
            'Fit' => array(
                'Slim fit',
                'Regular fit'
            ),
            'Blouse'
        ),
        'Jeans' => array(
            'Super skinny',
            'Slim',
            'Straight cut',
            'Loose',
            'Boot cut / flare'
        )
    ),
);

showCategories($categories);

function showCategories($cats,$depth=1) { // change depth to 0 to remove the first two -

    if(!is_array($cats))
        return false;

    foreach($cats as$key=>$val) {

        echo str_repeat("-",$depth*2).(is_string($key)?$key:$val).'<br>'; // updated this line so no warning or notice will get fired

        if(is_array($val)) {
            $depth++;
            showCategories($val,$depth);
            $depth--;
        }

    }

}

分类名称总是数组键吗?谢谢!我已经更新了-没有注意到拼写错误:D
--Fashion
----Shirts
------Sleeve
--------Short sleeve
--------Long sleeve
------Fit
--------Slim fit
--------Regular fit
------Blouse
----Jeans
------Super skinny
------Slim
------Straight cut
------Loose
------Boot cut / flare