?
Solved

Looping through multidimensional array

Posted on 2011-03-07
4
Medium Priority
?
296 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:jaxstorm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 14

Expert Comment

by:svgmuc
ID: 35056427
echo $a['comment'];

$a has a reference to the array item already.
0
 
LVL 3

Expert Comment

by:nickswanjan
ID: 35056463
Use the key, not the value. Try:

foreach ($contents as $key => $value) {
  echo $contents[$key]['comment'];
}
0
 
LVL 2

Accepted Solution

by:
adeelshahid earned 2000 total points
ID: 35056656
try this one, just copy paste it,

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

Author Closing Comment

by:jaxstorm
ID: 35059098
Aye that got it, cheers
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question