?
Solved

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

Posted on 2014-04-29
2
Medium Priority
?
257 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
[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
2 Comments
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
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

777 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