I haven't seen the rest of this thread so this may be irrelevant ... but

foreach($array as $key=>$value)
{       $array[$key] = "fred";
}

... alters an array while traversing it. Your case seems more complicated
but I would have thought nesting foreaches in this way would allow you to
access the original element.


foreach($array as $key1=>$value1)
{       foreach($array as $key2=>$value2)
{       $array[$key1][$key2] = "fred";
}
}

Tim
http://www.chessish.com <http://www.chessish.com> 


        ----------
        From:  Darren Gamble [SMTP:[EMAIL PROTECTED]]
        Sent:  04 December 2001 15:22
        To:  PHP List; 'Mike Eheler'
        Subject:  RE: [PHP] Multidimensional array construction

        Good day,

        Thanks to all who replied.

        This isn't quite what I needed, though.  I _have_ the array (or
delimited
        list would do, too).  What I need to do is _CREATE_ the array
element
        $myarray['foo']['bar']['green']['apple'] and set it to some value.
Actually
        traversing said array isn't hard, as you pointed out.

        foreach() doesn't work, as it just uses a copy of the original
array.  I
        think there might be some way to use variable references, but I
haven't
        gotten one to work yet.

        Any other suggestions?

        ============================
        Darren Gamble
        Planner, Regional Services
        Shaw Cablesystems GP
        630 - 3rd Avenue SW
        Calgary, Alberta, Canada
        T2P 4L4
        (403) 781-4948


        -----Original Message-----
        From: Mike Eheler [mailto:[EMAIL PROTECTED]]
        Sent: Monday, December 03, 2001 5:30 PM
        To: Martin Towell
        Cc: 'Darren Gamble'; PHP List
        Subject: Re: [PHP] Multidimensional array construction


        I did something like this recently. Here's how I did it:

        $some_value1 = 'Hello World';
        $myarray['foo']['bar']['green']['apple'] = $some_value1;

        function get_opt($arr, $keys,$sep=':') {
            $var = $arr;
            $tmp = split($sep,$keys);
            foreach ($tmp as $k => $v) {
                $var = $var[$v];
            }
            if (isset($var)) return $var;
            return '';
        }

        echo get_opt($myarray, 'foo:bar:green:apple');

        It needs refining, but it should do the job. That's entirely from 
        memory, mind you.. it should work, though.

        Mike

        Martin Towell wrote:

        >I was thinking that you could use a "pointer to var", eg:
        >    $var = 'myarray["foo"]["bar"]["red"]["apple"]';
        >    // this would obviously be created dynamically, hard coded for
testing
        >    $$var = $some_value1;
        >    echo $myarray["foo"]["bar"]["red"]["apple"];
        >but when I tried it, it didn't work :(
        >looks like eval() to the rescue...
        >
        >-----Original Message-----
        >From: Darren Gamble [mailto:[EMAIL PROTECTED]]
        >Sent: Tuesday, December 04, 2001 10:37 AM
        >To: PHP List
        >Subject: [PHP] Multidimensional array construction
        >
        >
        >Here's a question for the list:
        >
        >I have a two-dimensional array; essentially a list of arrays.  Each
element
        >(an array) can have any number of elements.  As a small example:
        >
        >(
        >  ( "foo" , "bar" , "red" , "apple" ),
        >  ( "foo" , "bar" , "red" , "car"),
        >  ( "foo" , "green" )
        >)
        >
        >I would like to traverse this array and place all of the data into
another
        >multidimensional array.  The following statements illustrate how
I'd like
        to
        >do this from the example:
        >
        >$myarray["foo"]["bar"]["red"]["apple"] = $some_value1;
        >$myarray["foo"]["bar"]["red"]["car"]   = $some_value2;
        >$myarray["foo"]["green"]               = $some_value3;
        >
        >Is there any way to easily do this in PHP?  I could "cheat" and use
eval(),
        >but there is probably a better way.  I have thought of using each()
or
        >references, but nothing has come to mind so far. 
        >
        >Any ideas?  Should I just use eval() ?
        >
        >============================
        >Darren Gamble
        >Planner, Regional Services
        >Shaw Cablesystems GP
        >630 - 3rd Avenue SW
        >Calgary, Alberta, Canada
        >T2P 4L4
        >(403) 781-4948
        >
        

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to