problem displaying all of an array

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
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

problem displaying all of an array

Post 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
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: problem displaying all of an array

Post 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'];
   }
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: problem displaying all of an array

Post 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'];
}
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.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: problem displaying all of an array

Post by IGGt »

cheers guys,

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