doufei5537 2014-05-19 17:22
浏览 44
已采纳

更加动态的输出多维数组PHP的方法

So, I am still learning PHP, and I wanted to customize the arrays a bit in matter of structure, so they are not static. But I've came to a problem. I've managed to write out each array that has an array inside of it, but now I struggle in finding a way of writing out the ones that do not have an array inside of them, and not to get 0-s and 1-s instead of the actual name of the array elements.

I basically need some advice on how to make this work properly, I wouldn't mind if you could tell me a more efficient way of writing everything out and solving the problem at the same time.

<?php 

$food = array(
'Healthy'=>array(   'Salad'=> array('Fruit Salad','Tropical Salad'), 
                    'Vegetables'=>array('Carrot','Cucumber'), 
                    'Pasta'),

'Unhealthy'=>array('Pizza','Ice Cream')
);

foreach ($food as $type=>$food_types) {
    echo '<b>'.$type.'</b>'.'<br>';
    if (is_array($food_types)) {
        foreach($food_types as $type_names=>$fruits) {
            if (isset($type_names) && !empty($type_names) && is_array($fruits)) {
                echo $type_names.'<br>';
                foreach ($fruits as $name_of_fruits) {
                    echo '<i>'.$name_of_fruits.'</i>'.'<br>';
                }
            } 
        }
    } else {
        echo $food_types;
    }
}

 ?>

**UPDATE*** Thanks to celeriko's comment I found the problem, being newbie to PHP I didn't really knew that the type of "Healthy Food" that was not an array was actually the first index of the big "Healthy Food" array, as the other two were array and didn't count as actual indexes.

Here is the fixed code, thank you cerleriko once again!

<?php 

$food = array(
'Healthy'=>array(   'Salad'=> array('Fruit Salad','Tropical Salad'), 
                    'Vegetables'=>array('Carrot','Cucumber'), 
                    'Pasta'),

'Unhealthy'=>array('Pizza','Ice Cream')
);



foreach ($food as $type=>$food_types) {
    echo '<b>'.$type.'</b>'.'<br>';
    if (is_array($food_types)) {
        foreach($food_types as $type_names=>$fruits) {
            if (isset($type_names) && !empty($type_names) && is_array($fruits)) {
                echo $type_names.'<br>';
                foreach ($fruits as $name_of_fruits) {
                    echo '<i>'.$name_of_fruits.'</i>'.'<br>';
                }
            } else {
                echo $fruits.'<br>'; /* Here I had to enter this line and everything works fine! */
            }
        }
    } else {
        echo $food_types;
    }
}

 ?>
  • 写回答

1条回答 默认 最新

  • doxp30826 2014-05-19 17:47
    关注

    Glad you got it fixed, but as for the question "More dynamic way of outputting Multi-Dimensional Arrays", here is a recursive one. It sets a level-X class to the div that you can style:

    function print_it($array) {
        static $level = 1;
    
        foreach($array as $k => $v) {
            if(!is_numeric($k)) {
                echo '<div class="level-'.$level.'">'.$k.'</div>'.PHP_EOL;
            }
            if(is_array($v)) {
                $level++;
                print_it($v);
                $level--;
            } else {
                echo '<div class="level-'.$level.'">'.$v.'</div>'.PHP_EOL;
            }
        }
    
    }
    
    print_it($food);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献