RE: [PHP] array/iteration issue!!
| From: | Robert Cummings | Date: | Fri, 28 Nov 2008 04:15:09 +0000 |
| Subject: | RE: [PHP] array/iteration issue!! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-283879@lists.php.net to get a copy of this message | ||
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:
<?php
$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.
--
http://www.interjinn.com
Application and Templating Framework for PHP