note 108689 deleted from control-structures.foreach by googleguy

From: Date: Wed, 14 Nov 2012 22:16:29 +0000
Subject: note 108689 deleted from control-structures.foreach by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-191937@lists.php.net to get a copy of this message
Note Submitter: peter at olds dot co ---- This is probably common sense to a lot of people but I spent close to two hours trying to figure this out. I had an array output that looked like this: <?php /* Array ( [0] => stdClass Object ( [event_id] => 1 [form_name] => Long Beach 2012 [form_id] => longbeach2012 [registrations] => 1 ) [1] => stdClass Object ( [event_id] => 2 [form_name] => Las Vegas 2013 [form_id] => lasvegas2013 [registrations] => 0 ) ) */ ?> Inside of the array are objects so you could call a single value by doing something like: <?php $arr[0]->event_id; // returns 1 ?> So to be able to do a foreach through it do the following: <?php $results = array(); // The array we're working with foreach( $results as $obj ) { foreach( $obj as $key => $value ) { echo $key; // returns event_id, form_name, etc. echo $value; // returns 1, Long Beach 2012 } } ?>

« previous php.notes (#191937) next »