Page 1 of 1

problem displaying all of an array

Posted: Mon Jan 24, 2011 11:30 am
by IGGt
I have an array that I have created (contains 4 sections in total - 1 database, 1 query, 4 lines in the result):

[MySQL06] => Array
(
[0] => Array
(
[id] => 14
[running] => completed
[dt] => 2011-01-24 15:54:23
)

[1] => Array
(
[id] => 15
[running] => completed
[dt] => 2011-01-24 15:54:24
)

But when I try to refer to them I get an error:

Code: Select all

foreach($instance2[] as $DB2) {
	$id = $DB2['id'];
	$running = $DB2['running'];
	$dt = $DB2['dt'];
};
Fatal error: Cannot use [] for reading in C:\wamp\www\ts\DBresult.php on line 28

How can I refer to the 4 sections accurately?
If I take the [] out I get errors like:

Notice: Undefined index: id in C:\wamp\www\ts\DBresult.php on line 30

Re: problem displaying all of an array

Posted: Mon Jan 24, 2011 12:59 pm
by mikecampbell
Since you have a multi-dimensional array, you will need to use more than one loop.

Code: Select all

foreach($instance2 as $DB2) {
   foreach($DB2 as $sub) {
        $id = $sub['id'];
        $running = $sub['running'];
        $dt = $sub['dt'];
   }
}

Re: problem displaying all of an array

Posted: Mon Jan 24, 2011 1:07 pm
by AbraCadaver
Actually, if you just have the one level then:

Code: Select all

foreach($instance2['MySQL06'] as $DB2) {
        $id = $DB2['id'];
        $running = $DB2['running'];
        $dt = $DB2['dt'];
}

Re: problem displaying all of an array

Posted: Wed Jan 26, 2011 4:15 am
by IGGt
cheers guys,

I will use the first one because going forward there will be multiple databases (MySQL06, MySQL07...).