RE: [PHP] array/iteration issue!!

From: Date: Fri, 28 Nov 2008 03:10:11 +0000
Subject: RE: [PHP] array/iteration issue!!
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-283876@lists.php.net to get a copy of this message
On Thu, 2008-11-27 at 18:55 -0800, bruce wrote: > hey robert.. > > ok.. so if i changed the array to have a dept1, and a dept2 > > $a=array("college"=> "foo", > "dept1"=>array("dept"=> "physics", > > "class"=>array("class1"=>"sss","class2"=>"sffgg") > ), > "dept2"=>array("dept"=> "english", > > "class"=>array("class1"=>"sss","class2"=>"sffgg") > ) > ); > how would i iterate through this..?? Your array is terribly structured. But the following provides traversal in the way you want: <?php $a = array ( "college" => "foo", "dept1" => array ( "dept" => "physics", "class" => array ( "class1" => "sss", "class2" => "sffgg" ) ), "dept2" => array ( "dept" => "english", "class" => array ( "class1" => "sss", "class2" => "sffgg" ) ) ); $college = $a['college']; foreach( $a as $deptKey => $deptInfo ) { if( strpos( $deptKey, 'dept' ) === 0 ) { $dept = $deptInfo['dept']; foreach( $deptInfo['class'] as $class ) { echo "$college, $dept, $class\n"; } } } ?> Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP

« previous php.general (#283876) next »