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
Solved

how can I modify foreach to iterate the values of an array or arrays

Posted on 2014-04-29
2
253 Views
Last Modified: 2014-04-29
<?php
  $configData= array("PersonModule" => array( 'person'=>'bob' ),"FtpModule"    => array( 'host' =>'example.com','user'  =>'anon' ));
  foreach ($configData as $name=>$value){
    echo '<br>name: '.$name.' value: '.$value;
  }
  

Open in new window


Notice: Array to string conversion in C:\Users\Acer\Documents\NuSphere PhpED\Projects\noname308.php on line 4

name: PersonModule value: Array
Notice: Array to string conversion in C:\Users\Acer\Documents\NuSphere PhpED\Projects\noname308.php on line 4

name: FtpModule value: Array


how can I modify foreach to iterate the values of an array or arrays
0
Comment
Question by:rgb192
2 Comments
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 500 total points
ID: 40031107
You would need to set up a secondary foreach to handle the second array:

<?php
  $configData= array("PersonModule" => array( 'person'=>'bob' ),"FtpModule"    => array( 'host' =>'example.com','user'  =>'anon' ));
  foreach ($configData as $name=>$array){
      foreach ($array as $key => $value) {
    echo '<br>name: '.$name.' value: '.$value;  // or echo '<br>name: ' . $name.' key: '.$key.' value: '.$value;
      }
  }
   

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 40031151
thanks for foreach array
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
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.

829 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