Solved

Looping through multidimensional array

Posted on 2011-03-07
4
293 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 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 look for a specific file type in a local or remote server directory using PHP.

730 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