RE: [PHP] array/iteration issue!!
both ways work...!!! this is a really simple/quick issue to populate some test db tbls.. i've only played with multi php arrays (sp??) in passing... and my days of being a serious eng/software guy are long over!! although.. with this economy! thanks -Original Message- From: Robert Cummings [mailto:[EMAIL PROTECTED] Sent: Thursday, November 27, 2008 8:40 PM To: Micah Gersten Cc: bruce; 'PHP General list' Subject: Re: [PHP] array/iteration issue!! On Thu, 2008-11-27 at 22:22 -0600, Micah Gersten wrote: > > This is actually a much smaller data structure. > > $colleges = array > ( > 'Blah Blah University' => > array > ( > 'physics' => array > ( > 'sss', > 'sffgg', > ), > 'english' => array > ( > 'sss', > 'sffgg', > ) > ), > 'Glah Gleh University' => > array > ( > 'physics' => array > ( > 'sss', > 'sffgg', > ), > 'english' => array > ( > 'sss', > 'sffgg', > ), > ) > ); > > > foreach( $colleges as $collegeName => $depts ) > { > foreach( $depts as $deptName => $classes) > { > foreach( $classes as $className ) > { > echo "$collegeName, $deptName, $className\n"; > } > } > } Yes, I thought of that one too, but it's less flexible. What if you need to add other fields to the college, or department. Then you'd need to redo the whole structure to something more similar to what I did. Since bruce was having issues, I gave him a flexible format that could handle other fields if they arose. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array/iteration issue!!
On Thu, 2008-11-27 at 22:22 -0600, Micah Gersten wrote: > > This is actually a much smaller data structure. > > $colleges = array > ( > 'Blah Blah University' => > array > ( > 'physics' => array > ( > 'sss', > 'sffgg', > ), > 'english' => array > ( > 'sss', > 'sffgg', > ) > ), > 'Glah Gleh University' => > array > ( > 'physics' => array > ( > 'sss', > 'sffgg', > ), > 'english' => array > ( > 'sss', > 'sffgg', > ), > ) > ); > > > foreach( $colleges as $collegeName => $depts ) > { > foreach( $depts as $deptName => $classes) > { > foreach( $classes as $className ) > { > echo "$collegeName, $deptName, $className\n"; > } > } > } Yes, I thought of that one too, but it's less flexible. What if you need to add other fields to the college, or department. Then you'd need to redo the whole structure to something more similar to what I did. Since bruce was having issues, I gave him a flexible format that could handle other fields if they arose. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array/iteration issue!!
much props guys!!! thanks!! -Original Message- From: Micah Gersten [mailto:[EMAIL PROTECTED] Sent: Thursday, November 27, 2008 8:23 PM To: Robert Cummings Cc: bruce; 'PHP General list' Subject: Re: [PHP] array/iteration issue!! Robert Cummings wrote: > On Thu, 2008-11-27 at 19:36 -0800, bruce wrote: > >> hey robert!! >> >> thanks. and yeah, you're right, it's not the best.. so tell me, given that >> i'm ripping through this on the fly, and i can have the structure in any way >> i choose. this is just to simulate/populate some test tbls.. what's a better >> way to create an array structure to have a collegename, followed by some >> deptnames, followed by some classnames for the depts... >> >> perhaps something like this?? >> >> $a = array >> ( >> "college" => "foo", >> array >> ( >> "dept" => "physics", >> "class" => array >> ( >> "class1" => "sss", >> "class2" => "sffgg" >> ) >> ), >> array >> ( >> "dept" => "english", >> "class" => array >> ( >> "class1" => "sss", >> "class2" => "sffgg" >> ) >> ) >> ); >> > > Not quite. The following is probably what you want: > > > $colleges = array > ( > array > ( > 'name' => 'Blah Blah University', > 'depts' => array > ( > array > ( > 'name'=> 'physics', > 'classes' => array > ( > 'sss', > 'sffgg', > ), > ), > array > ( > 'name'=> 'english', > 'classes' => array > ( > 'sss', > 'sffgg', > ), > ), > ), > ), > array > ( > 'name' => 'Glah Gleh University', > 'depts' => array > ( > array > ( > 'name'=> 'physics', > 'classes' => array > ( > 'sss', > 'sffgg', > ), > ), > array > ( > 'name'=> 'english', > 'classes' => array > ( > 'sss', > 'sffgg', > ), > ), > ), > ), > ); > > foreach( $colleges as $college ) > { > $collegeName = $college['name']; > foreach( $college['depts'] as $dept ) > { > $deptName = $dept['name']; > foreach( $dept['classes'] as $className ) > { > echo "$collegeName, $deptName, $className\n"; > } > } > } > > ?> > > Cheers, > Rob. > This is actually a much smaller data structure. $colleges = array ( 'Blah Blah University' => array ( 'physics' => array ( 'sss', 'sffgg', ), 'english' => array ( 'sss', 'sffgg', ) ), 'Glah Gleh University' => array ( 'physics' => array ( 'sss', 'sffgg', ), 'english' => array ( 'sss', 'sffgg', ), ) ); foreach( $colleges as $collegeName => $depts ) { foreach( $depts as $deptName => $classes) { foreach( $classes as $className ) { echo "$collegeName, $deptName, $className\n"; } } } Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array/iteration issue!!
Robert Cummings wrote: > On Thu, 2008-11-27 at 19:36 -0800, bruce wrote: > >> hey robert!! >> >> thanks. and yeah, you're right, it's not the best.. so tell me, given that >> i'm ripping through this on the fly, and i can have the structure in any way >> i choose. this is just to simulate/populate some test tbls.. what's a better >> way to create an array structure to have a collegename, followed by some >> deptnames, followed by some classnames for the depts... >> >> perhaps something like this?? >> >> $a = array >> ( >> "college" => "foo", >> array >> ( >> "dept" => "physics", >> "class" => array >> ( >> "class1" => "sss", >> "class2" => "sffgg" >> ) >> ), >> array >> ( >> "dept" => "english", >> "class" => array >> ( >> "class1" => "sss", >> "class2" => "sffgg" >> ) >> ) >> ); >> > > Not quite. The following is probably what you want: > > > $colleges = array > ( > array > ( > 'name' => 'Blah Blah University', > 'depts' => array > ( > array > ( > 'name'=> 'physics', > 'classes' => array > ( > 'sss', > 'sffgg', > ), > ), > array > ( > 'name'=> 'english', > 'classes' => array > ( > 'sss', > 'sffgg', > ), > ), > ), > ), > array > ( > 'name' => 'Glah Gleh University', > 'depts' => array > ( > array > ( > 'name'=> 'physics', > 'classes' => array > ( > 'sss', > 'sffgg', > ), > ), > array > ( > 'name'=> 'english', > 'classes' => array > ( > 'sss', > 'sffgg', > ), > ), > ), > ), > ); > > foreach( $colleges as $college ) > { > $collegeName = $college['name']; > foreach( $college['depts'] as $dept ) > { > $deptName = $dept['name']; > foreach( $dept['classes'] as $className ) > { > echo "$collegeName, $deptName, $className\n"; > } > } > } > > ?> > > Cheers, > Rob. > This is actually a much smaller data structure. $colleges = array ( 'Blah Blah University' => array ( 'physics' => array ( 'sss', 'sffgg', ), 'english' => array ( 'sss', 'sffgg', ) ), 'Glah Gleh University' => array ( 'physics' => array ( 'sss', 'sffgg', ), 'english' => array ( 'sss', 'sffgg', ), ) ); foreach( $colleges as $collegeName => $depts ) { foreach( $depts as $deptName => $classes) { foreach( $classes as $className ) { echo "$collegeName, $deptName, $className\n"; } } } Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array/iteration issue!!
On Thu, 2008-11-27 at 19:36 -0800, bruce wrote: > hey robert!! > > thanks. and yeah, you're right, it's not the best.. so tell me, given that > i'm ripping through this on the fly, and i can have the structure in any way > i choose. this is just to simulate/populate some test tbls.. what's a better > way to create an array structure to have a collegename, followed by some > deptnames, followed by some classnames for the depts... > > perhaps something like this?? > > $a = array > ( > "college" => "foo", > array > ( > "dept" => "physics", > "class" => array > ( > "class1" => "sss", > "class2" => "sffgg" > ) > ), > array > ( > "dept" => "english", > "class" => array > ( > "class1" => "sss", > "class2" => "sffgg" > ) > ) > ); Not quite. The following is probably what you want: 'Blah Blah University', 'depts' => array ( array ( 'name'=> 'physics', 'classes' => array ( 'sss', 'sffgg', ), ), array ( 'name'=> 'english', 'classes' => array ( 'sss', 'sffgg', ), ), ), ), array ( 'name' => 'Glah Gleh University', 'depts' => array ( array ( 'name'=> 'physics', 'classes' => array ( 'sss', 'sffgg', ), ), array ( 'name'=> 'english', 'classes' => array ( 'sss', 'sffgg', ), ), ), ), ); foreach( $colleges as $college ) { $collegeName = $college['name']; foreach( $college['depts'] as $dept ) { $deptName = $dept['name']; foreach( $dept['classes'] as $className ) { echo "$collegeName, $deptName, $className\n"; } } } ?> Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array/iteration issue!!
hey robert!! thanks. and yeah, you're right, it's not the best.. so tell me, given that i'm ripping through this on the fly, and i can have the structure in any way i choose. this is just to simulate/populate some test tbls.. what's a better way to create an array structure to have a collegename, followed by some deptnames, followed by some classnames for the depts... perhaps something like this?? $a = array ( "college" => "foo", array ( "dept" => "physics", "class" => array ( "class1" => "sss", "class2" => "sffgg" ) ), array ( "dept" => "english", "class" => array ( "class1" => "sss", "class2" => "sffgg" ) ) ); -Original Message- From: Robert Cummings [mailto:[EMAIL PROTECTED] Sent: Thursday, November 27, 2008 7:10 PM To: bruce Cc: 'PHP General list' Subject: RE: [PHP] array/iteration issue!! 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: "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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array/iteration issue!!
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: "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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] array/iteration issue!!
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..?? thanks -Original Message- From: Robert Cummings [mailto:[EMAIL PROTECTED] Sent: Thursday, November 27, 2008 6:18 PM To: bruce Cc: 'PHP General list' Subject: Re: [PHP] array/iteration issue!! On Thu, 2008-11-27 at 17:31 -0800, bruce wrote: > hi. > > i have the following test multidiminsional array. i'm trying to figure out > how to iterate through the array, to produce something like > > foo, physics, sss > foo, physics, sffgg > foo, english, sss > foo, english, sffgg > > can't quite seem to get it right!! > > thoughts/comments... etc... > > thanks > > > $a=array("college"=> "foo", >"dept"=>array("dept"=> "physics", > "class"=>array("class1"=>"sss","class2"=>"sffgg") > ), > "dept"=>array("dept"=> "english", > "class"=>array("class1"=>"sss","class2"=>"sffgg") > ) > ); You can't. You're array is valid but the second 'dept' key overwrites the first. Thus the physics dept is lost. Check it for yourself... print_r( $a ) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] array/iteration issue!!
On Thu, 2008-11-27 at 17:31 -0800, bruce wrote: > hi. > > i have the following test multidiminsional array. i'm trying to figure out > how to iterate through the array, to produce something like > > foo, physics, sss > foo, physics, sffgg > foo, english, sss > foo, english, sffgg > > can't quite seem to get it right!! > > thoughts/comments... etc... > > thanks > > > $a=array("college"=> "foo", >"dept"=>array("dept"=> "physics", > "class"=>array("class1"=>"sss","class2"=>"sffgg") > ), > "dept"=>array("dept"=> "english", > "class"=>array("class1"=>"sss","class2"=>"sffgg") > ) > ); You can't. You're array is valid but the second 'dept' key overwrites the first. Thus the physics dept is lost. Check it for yourself... print_r( $a ) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] array/iteration issue!!
hi. i have the following test multidiminsional array. i'm trying to figure out how to iterate through the array, to produce something like foo, physics, sss foo, physics, sffgg foo, english, sss foo, english, sffgg can't quite seem to get it right!! thoughts/comments... etc... thanks $a=array("college"=> "foo", "dept"=>array("dept"=> "physics", "class"=>array("class1"=>"sss","class2"=>"sffgg") ), "dept"=>array("dept"=> "english", "class"=>array("class1"=>"sss","class2"=>"sffgg") ) ); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php