RE: array/iteration issue!!

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

 



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


-- 
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