PHP recursion print all elements of a multidimensional array with keys

前端 未结 5 698
暗喜
暗喜 2020-12-11 20:26

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, the

相关标签:
5条回答
  • 2020-12-11 21:01

    What's wrong with print_r, var_dump or var_export?

    That aside, read the documentation on foreach and you'll clearly see how to get the key you're iterating over.

    0 讨论(0)
  • 2020-12-11 21:01
    function printAll($a) {
      foreach ($a as $k => $v) {
        echo $k, ' ';
      }
    
      printAllVals($a);
    }
    
    function printAllVals($a) {
      if (!is_array($a)) {
        echo $a, ' ';
          return;
       }
    
       foreach($a as $k => $v) {
         if ($k < 10) {
           printAllVals($v);
         }
       }
    }
    
    0 讨论(0)
  • 2020-12-11 21:14

    I am assuming you want something non-programming humans can make some sort of sense out of.

    function pretty_dump($arr, $d=1){
        if ($d==1) echo "<pre>";    // HTML Only
        if (is_array($arr)){
            foreach($arr as $k=>$v){
                for ($i=0;$i<$d;$i++){
                    echo "\t";
                }
                if (is_array($v)){
                    echo $k.PHP_EOL;
                    Pretty_Dump($v, $d+1);
                } else {
                    echo $k."\t".$v.PHP_EOL;
                }
            }
        }
        if ($d==1) echo "</pre>";   // HTML Only
    }
    

    Usage:

    $myarray=array(
        'mammals'=>array(
            'cats'=>array(
                'cheetah',
                'lion',
                'cougar'
            ),
            'dogs'=>array(
                'big'=>'Scooby',
                'small'=>'chihuahua',
                'medium'=>array(
                    'pumi',
                    'bulldog',
                    'boxer'
                )
            ),
        ),
        'fish'=>'fish',
        'birds'=>array(
            'flying'=>array(
                'mallard',
                'condor',
                'gull'
            ),
            'nonflying'=>'emu'
        )
    );
    
    pretty_dump($myarray);
    

    Output:

        mammals
            cats
                0   cheetah
                1   lion
                2   cougar
            dogs
                big Scooby
                small   chihuahua
                medium
                    0   pumi
                    1   bulldog
                    2   boxer
        fish    fish
        birds
            flying
                0   mallard
                1   condor
                2   gull
            nonflying   emu
    
    0 讨论(0)
  • 2020-12-11 21:14

    Try with:

    foreach($a as $k => $v)
    

    where $k is your key and $v is still value.

    0 讨论(0)
  • 2020-12-11 21:26
    function printAll($a) {
        if (!is_array($a)) {
            echo $a, ' ';
            return;
        }
    
        foreach($a as $k => $value) {
             if($k<10){
                 printAll($k);
                 printAll($value);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题