Re: array/iteration issue!!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux