RE: [PHP] array/iteration issue!!
| From: | bruce | Date: | Fri, 28 Nov 2008 04:37:43 +0000 |
| Subject: | RE: [PHP] array/iteration issue!! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-283882@lists.php.net to get a copy of this message | ||
much props guys!!!
thanks!!
-----Original Message-----
From: Micah Gersten [mailto:micah@onshore.com]
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:
>
> <?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.
>
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