PHP递归使用键打印多维数组的所有元素 [英] PHP recursion print all elements of a multidimensional array with keys

查看:172
本文介绍了PHP递归使用键打印多维数组的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了以下代码,该代码可以很好地打印数组的所有元素.如何修改它以一次打印一个键,然后打印对应于该键的所有值,然后打印另一个键,再打印对应于该键的所有值?我还想对其进行修改,以便它只为每个键打印前9个值(不超过此值).

I found the following code, which prints all the elements of an array fine. How can I modify it to print a key one time and then all the values corresponding to the key, then another key, then all values corresponding to key? I also would like to modify it so it only prints the first 9 values (no more than this) for each key.

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

   foreach($a as $v) {
   printAll($v);
  }
 }

推荐答案

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

    foreach($a as $k => $value) {
         if($k<10){
             printAll($k);
             printAll($value);
        }
    }
}

这篇关于PHP递归使用键打印多维数组的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆