note 108689 added to control-structures.foreach

From: Date: Thu, 17 May 2012 20:12:45 +0000
Subject: note 108689 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-188044@lists.php.net to get a copy of this message
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 } } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 208.76.193.226 ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/108689 Del: integrated -- https://master.php.net/note/delete/108689/integrated Del: useless -- https://master.php.net/note/delete/108689/useless Del: bad code -- https://master.php.net/note/delete/108689/bad+code Del: spam -- https://master.php.net/note/delete/108689/spam Del: non-english -- https://master.php.net/note/delete/108689/non-english Del: in docs -- https://master.php.net/note/delete/108689/in+docs Del: other reasons-- https://master.php.net/note/delete/108689 Reject -- https://master.php.net/note/reject/108689 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#188044) next »