RE: [PHP] Multidimensional array construction
| From: | Jim | Date: | Tue, 04 Dec 2001 00:29:21 +0000 |
| Subject: | RE: [PHP] Multidimensional array construction | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-76439@lists.php.net to get a copy of this message | ||
oops. That's not correct. I wish it was though! ;)
This might be interesting ... The function extract() allows you to extract all values from an array and prefix them with a specified string. What I didn't know until just a second ago was that you can supply a function as a string, so ... $my_array = array("a","b","c","d",array("a","b","c","d")); $i = 1; extract($my_array,EXTR_PREFIX_ALL,"SOMEVAL".$i++); print "<pre>"; print $SOMEVAL_1; print $SOMEVAL_2; print $SOMEVAL_3; print $SOMEVAL_4; print $SOMEVAL_5; print "</pre>"; ... will produce: a b c d [Array] Which is cool. Not quite what you wanted, but maybe you could run with it. Jim-- Jim Musil --------- Multimedia Programmer Nettmedia ------------- 212-629-0004 jim@nettmedia.com-----Original Message----- From: Darren Gamble [mailto:Darren.Gamble@sjrb.ca] 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;-- Jim Musil --------- Multimedia Programmer Nettmedia ------------- 212-629-0004 jim@nettmedia.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net$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: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net