Looping through multidimensional array

I have a PHP array, $contents like this

        Array
    (
        [0] => Array
            (
                [comment] =>
                [kickstart] => /var/lib/cough/cough/wheeze.cfg
               
            )

Obviously this is quite heavily populated. I want to pull out all the "comments" from the array.

I tried this:

foreach ($contents as $key => $a) {
echo $contents[$a]['comment'];
}

but didn't work.

Any help appreciated.
LVL 8
jaxstormAsked:
Who is Participating?
 
adeelshahidCommented:
try this one, just copy paste it,

foreach ($contents as $content) {
      echo $content['comment'] . '<br />';
}
0
 
svgmucCommented:
echo $a['comment'];

$a has a reference to the array item already.
0
 
nickswanjanCommented:
Use the key, not the value. Try:

foreach ($contents as $key => $value) {
  echo $contents[$key]['comment'];
}
0
 
jaxstormAuthor Commented:
Aye that got it, cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.