silkfire Posted October 11, 2011 Share Posted October 11, 2011 How do you solve this problem? I have a flat array that I need to transform into a 2D array where every fourth item is group together and contains 4 items. From: array( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ) To: array( array(0, 4, 8, 12), array(1, 5, 9, 13), array(2, 6, 10, 14), array(3, 7, 11, 15) ) I'm sure there's some easy solution... Note that I need to transform it, not create one from scratch. Quote Link to comment https://forums.phpfreaks.com/topic/248879-regrouping-arrays/ Share on other sites More sharing options...
WebStyles Posted October 11, 2011 Share Posted October 11, 2011 hmmm... array_chunk will allow you to split into groups of 4 elements, but will maintain the same order as the original array... You could then re-organize them with a simple for() loop using the keys 0,1,2 and 3. How is the original array built? (what values will it contain? just a sequence of numbers like your example? or can it contain other values or non-sequential values?) Quote Link to comment https://forums.phpfreaks.com/topic/248879-regrouping-arrays/#findComment-1278096 Share on other sites More sharing options...
silkfire Posted October 11, 2011 Author Share Posted October 11, 2011 It will contain strings, does it matter? But they have to be grouped together logically, like I showed. Quote Link to comment https://forums.phpfreaks.com/topic/248879-regrouping-arrays/#findComment-1278099 Share on other sites More sharing options...
WebStyles Posted October 11, 2011 Share Posted October 11, 2011 well, yes, it *did* matter. If it were always a sequence of numbers I would suggest a different approach. In this case, try my first suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/248879-regrouping-arrays/#findComment-1278103 Share on other sites More sharing options...
silkfire Posted October 11, 2011 Author Share Posted October 11, 2011 Well actually what I showed you are the keys. What is your suggestion? Is there a way I could make a generic function where I can enter arguments for size of the groups and which Nth number to pick? Quote Link to comment https://forums.phpfreaks.com/topic/248879-regrouping-arrays/#findComment-1278107 Share on other sites More sharing options...
WebStyles Posted October 11, 2011 Share Posted October 11, 2011 c'mon, just try what I suggested first, and then worry about wrapping it into a function. <?php $initialArray = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); $chunks = array_chunk($initialArray, 4); $final = array(); $key = 0; foreach($chunks as $k=>$chunk){ for($i=0;$i<4;$i++){ $final[$i][$key] = $chunk[$i]; } $key++; } echo '<br>'; print_r($final); ?> Quote Link to comment https://forums.phpfreaks.com/topic/248879-regrouping-arrays/#findComment-1278109 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.