By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 435,144 Members | 972 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,144 IT Pros & Developers. It's quick & easy.

Array of 'n' dimensions?

 P: n/a Hi all, Wondering if anyone can help me. If i have an associative array: \$arr = array( "one" =array(1, 2, 3), "two" =array(5, 6), "three" =array(7,8,9,10) .... "n" =array(p,q,r....) ); and I want to combine the values for each value in the associative array to produce one array of "n" dimensions, how is this best done? The number of dimensions of the array is not known until runtime, so addressing the array can't be done in a hard-coded fashion. To exemplify what i'm after as the end result (in the case of there being 3 values in \$arr, as above); \$multi_arr = array( 1 =array( 5 =array ( 7 ="value", 8 ="value", 9 ="value", 10 ="value" ), 6 =array ( 7 ="value", 8 ="value", 9 ="value", 10 ="value" ) ), 2 =array ( 5 =array ( 7 ="value", 8 ="value", 9 ="value", 10 ="value" ), 6 =array ( 7 ="value", 8 ="value", 9 ="value", 10 ="value" ) ), 3 =array ( 5 =array ( 7 ="value", 8 ="value", 9 ="value", 10 ="value" ), 6 =array ( 7 ="value", 8 ="value", 9 ="value", 10 ="value" ) ) ); At the moment, I'm using a loop to build the necessary number of array subscripts in a string, then eval()ing the string, as a means of both creating and addressing the array. Which works, but it's a sloppy approach. I'm just wondering if anyone has another solution to the problem? Any help appreciated. My apologies for the length of the post. Regards Miles Jan 9 '07 #1
Share this Question
2 Replies

 P: n/a Miles wrote: Hi all, Wondering if anyone can help me. If i have an associative array: \$arr = array( "one" =array(1, 2, 3), "two" =array(5, 6), "three" =array(7,8,9,10) .... "n" =array(p,q,r....) ); and I want to combine the values for each value in the associative array to produce one array of "n" dimensions, how is this best done? The number of dimensions of the array is not known until runtime, so addressing the array can't be done in a hard-coded fashion. At the moment, I'm using a loop to build the necessary number of array subscripts in a string, then eval()ing the string, as a means of both creating and addressing the array. Which works, but it's a sloppy approach. I'm just wondering if anyone has another solution to the problem? Any help appreciated. My apologies for the length of the post. Regards Miles function arrMerge(\$arr) { \$list = array(); \$keys = array_keys(\$arr); foreach(\$keys as \$each) { if(is_array(\$arr[\$each])) \$list = array_merge(\$list, arrMerge(\$arr[\$each])); else array_push(\$list, \$arr[\$each]); } return \$list; } Hendri Kurniawan Jan 10 '07 #2

 P: n/a I think this does it where array values become array keys... \$arrays = array( 'any'=>array(11,22), 'key'=>array(33,44), 'will'=>array(55,66), 'work'=>array(77,88) ); \$multi = ''; \$arrays = array_reverse(\$arrays, true); foreach(\$arrays as \$array){ \$tmp = array(); foreach(\$array as \$val){ \$tmp[\$val] = \$multi; } \$multi = \$tmp; } print_r(\$multi); Jan 10 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.