Solved

Looping through multidimensional array

Posted on 2011-03-07
4
290 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
4 Comments
 
LVL 14

Expert Comment

by:svgmuc
Comment Utility
echo $a['comment'];

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

Expert Comment

by:nickswanjan
Comment Utility
Use the key, not the value. Try:

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

Accepted Solution

by:
adeelshahid earned 500 total points
Comment Utility
try this one, just copy paste it,

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

Author Closing Comment

by:jaxstorm
Comment Utility
Aye that got it, cheers
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now