4

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);
  }
 }
6
  • foreach($a as $k=>$v) { printAll($k); } ? Commented Dec 22, 2012 at 21:12
  • 7 questions, 11 answers, none accepted? Commented Dec 22, 2012 at 21:15
  • @ Steven Farley It's not letting me accept for another 2 minutes. Commented Dec 22, 2012 at 21:24
  • @user1605871 You should do this for the other 7 questions, you can find the questions you have asked in the past by visiting your profile Commented Dec 22, 2012 at 21:25
  • @ Steven Farley. Will do. Sry I didn't realize you could do that. Commented Dec 22, 2012 at 21:26

5 Answers 5

12

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
Sign up to request clarification or add additional context in comments.

Comments

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

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

1 Comment

That only print key values. Not the values. What about printing the values now?
2

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.

Comments

2
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);
     }
   }
}

4 Comments

What about limiting less than 9 values per key?
That works. Except one slight problem. I want to be able to print the key, then the first 9 values in key. Then print the next key, then the first 9 values in the key. Right now, the code prints all keys, then all values. How would I change this?
Your question says, "to print the keys of the arrays one time and then all the values".
Sry, I meant one key, then 9 values, another key, then 9 values.
0

Try with:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.