Solved

Looping through multidimensional array

Posted on 2011-03-07
4
292 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
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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Syntax Error 4 32
Wordpress Security 29 48
PHP Warning:  mysqli_real_escape_string() expects exactly 2 parameters 4 25
PHP Curl to output a url 7 47
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 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