Quick Array question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Katsu Webs
Forum Newbie
Posts: 5
Joined: Fri Dec 04, 2009 2:20 pm

Quick Array question

Post by Katsu Webs »

Howdy,

I was wondering if it is possible to loop through a multidimensional array using the foreach function.

The array in question is structured as such:

Array
(
[1] => Array
(
[key1] => value
[key2] => value
[key3] => value
[key4] => value
[key5] => value
[key6] => value
[key7] => value
)

The first array is a numerical index with the second array representing a row of data to be loaded in to a database. I want to loop through the main array and for each index output each key => value pair then upload to database and repeat.

Is this possible with a foreach statement?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Quick Array question

Post by AbraCadaver »

Assuming your array is called $array:

Code: Select all

foreach($array as $data) {
   foreach($data as $key => $value) {
      echo $key . " = " . $value;
   }
}
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Katsu Webs
Forum Newbie
Posts: 5
Joined: Fri Dec 04, 2009 2:20 pm

Re: Quick Array question

Post by Katsu Webs »

Ah ha,

many thanks Shawn code now working nicely! :D
Post Reply