MrXHellboy Posted November 23, 2010 Share Posted November 23, 2010 Hi all, Ok, simple question... just imagine a multidimensional array, array(array(1,2,3,array(4,5,6, array(7,8,9)))); how to build a recursive function that will unpack all the arrays into 1 array. Somehow, none of my tries has worked out properly Cheers Quote Link to comment Share on other sites More sharing options...
seanlim Posted November 23, 2010 Share Posted November 23, 2010 <?php function array_unpack($array) { $new_array = array(); foreach($array as $key=>$val) if(is_array($val)) $new_array = array_merge($new_array, array_unpack($val)); else $new_array[] = $val; return $new_array; } var_dump(array_unpack(array(array(1,2,3,array(4,5,6, array(7,8,9)))))); ?> Quote Link to comment Share on other sites More sharing options...
MrXHellboy Posted November 23, 2010 Author Share Posted November 23, 2010 Thx! Any other possibility for this ? I want to use the keys as well, and add a prefix to the key accordingly.... Array_merge does not support key options in this way as far as i know. So: array(array('Key' => 1,2,3,array(4,5,6, array('Key' => 7,8,9)))) the second key 'Key' must get a prefix.... is not really possible with array_merge ?! Cheers Quote Link to comment Share on other sites More sharing options...
seanlim Posted November 23, 2010 Share Posted November 23, 2010 the second key 'Key' must get a prefix.... can you clarify, prefixed with what exactly? Quote Link to comment Share on other sites More sharing options...
MrXHellboy Posted November 23, 2010 Author Share Posted November 23, 2010 Lets say, 'prefix' for example. Doesn't really matter, as long as they all will be in the new array. Quote Link to comment Share on other sites More sharing options...
seanlim Posted November 23, 2010 Share Posted November 23, 2010 this? function array_unpack($array, $str="") { $new_array = array(); foreach($array as $key=>$val) { if(is_array($val)) $new_array = array_merge($new_array, array_unpack($val, $str.$key.".")); else if(is_int($key)) $new_array[] = $val; else $new_array[$str.$key] = $val; } return $new_array; } Quote Link to comment Share on other sites More sharing options...
MrXHellboy Posted November 23, 2010 Author Share Posted November 23, 2010 Unfortunately no. Your first snippet was Ok. Everything was cool! But now, the second index 'key' needs to become prefix_key because there was already an index called 'key'. Quote Link to comment Share on other sites More sharing options...
MrXHellboy Posted November 23, 2010 Author Share Posted November 23, 2010 And 1 thing more. Why doesn't need $new_array = array_merge($new_array, array_unpack($val, $str.$key.".")); brackets and $new_array[] = $val; does ? It seems like you are overwriting this variable each time, but it doesnt... So what the big deal there Quote Link to comment Share on other sites More sharing options...
seanlim Posted November 24, 2010 Share Posted November 24, 2010 array_merge returns the the merged array, so we assign the entire array back into $array_new in the second statement, we are appending $val to the next integer key in $new_array Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.