Page 1 of 1

Quick Array question

Posted: Sat Dec 05, 2009 5:32 am
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?

Re: Quick Array question

Posted: Sat Dec 05, 2009 1:34 pm
by AbraCadaver
Assuming your array is called $array:

Code: Select all

foreach($array as $data) {
   foreach($data as $key => $value) {
      echo $key . " = " . $value;
   }
}
-Shawn

Re: Quick Array question

Posted: Sat Dec 05, 2009 4:18 pm
by Katsu Webs
Ah ha,

many thanks Shawn code now working nicely! :D