RE: array/iteration issue!!

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

 



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:robert@xxxxxxxxxxxxx]
Sent: Thursday, November 27, 2008 7:10 PM
To: bruce
Cc: 'PHP General list'
Subject: RE:  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:

<?php

$a = array
(
    "college" => "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


[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