felito Posted March 12, 2012 Share Posted March 12, 2012 I have this code $nArr = array('A', 'B', 'C', 'D', 'E', 'F'); $counter = 3; while ($counter > 0) { $chunkedValues[$counter][0] = 1; for ($j = 0 ; $j < $counter ; $j++) { $chunkedValues[$counter][$j + 1] = $nArr[$j]; } $nArr = array_slice($nArr, $counter--); } var_dump($chunkedValues); that outputs: array 3 => array 0 => int 1 1 => string 'A' (length=1) 2 => string 'B' (length=1) 3 => string 'C' (length=1) 2 => array 0 => int 1 1 => string 'D' (length=1) 2 => string 'E' (length=1) 1 => array 0 => int 1 1 => string 'F' (length=1) But i need this index structure: array 0 => array 0 => int 1 1 => string 'A' (length=1) 2 => string 'B' (length=1) 3 => string 'C' (length=1) 1 => array 1 => int 1 2 => string 'D' (length=1) 3 => string 'E' (length=1) 2 => array 2 => int 1 3 => string 'F' (length=1) I want to avoid loops with ceil. Any idea? thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/258750-array-output-order-in-the-index-structure/ Share on other sites More sharing options...
Muddy_Funster Posted March 12, 2012 Share Posted March 12, 2012 ksort($chunkedValues); Quote Link to comment https://forums.phpfreaks.com/topic/258750-array-output-order-in-the-index-structure/#findComment-1326464 Share on other sites More sharing options...
felito Posted March 12, 2012 Author Share Posted March 12, 2012 Well, as i said, i need this structure: array 0 => array 0 => int 1 1 => string 'A' (length=1) 2 => string 'B' (length=1) 3 => string 'C' (length=1) 1 => array 1 => int 1 2 => string 'D' (length=1) 3 => string 'E' (length=1) 2 => array 2 => int 1 3 => string 'F' (length=1) How ksort can be useful? Quote Link to comment https://forums.phpfreaks.com/topic/258750-array-output-order-in-the-index-structure/#findComment-1326465 Share on other sites More sharing options...
Muddy_Funster Posted March 12, 2012 Share Posted March 12, 2012 my bad, should read more carefully. here this looks like what you want, I lifted this directly from the php manual : I was trying to figure out how to normalize an array with numerical keys. Since I was doing for() for a lot of things, but only replacing it if the conditions were right, I wound up with off ball arrays I couldn't access. That being said, I looked for a method of normalizing the array and couldn't find one, so I built my own. I'm not sure how to go about making it recursive, but I didn't need that feature for my own, so I just went without recursion. //This will take array([5] => "test1", [4] => "test2", [9] => "test3") into array([0] => "test1", [1] => "test2", [2] => "test3") so you can access it easier. function normalize_array($array){ $newarray = array(); $array_keys = array_keys($array); $i=0; foreach($array_keys as $key){ $newarray[$i] = $array[$key]; $i++; } return $newarray; } Quote Link to comment https://forums.phpfreaks.com/topic/258750-array-output-order-in-the-index-structure/#findComment-1326468 Share on other sites More sharing options...
cpd Posted March 12, 2012 Share Posted March 12, 2012 I did it the following way. It does comes out fine the other end. I'm almost certain there will be an easier way to do it though as this seems a little long winded. Don't have time to do any research though. $nArr = array('A', 'B', 'C', 'D', 'E', 'F'); // Set some vars $length = 1; $offset = 0; $total = 0; // Reverse the array $nArr = array_reverse($nArr); // Create the array $chunkedValues = array(); while($total < count($nArr)){ $chunkedValues[] = array_slice($nArr, $total, $length); $total+= $length; $length++; } // Revers stuff. foreach($chunkedValues as $cK => $cV){ $chunkedValues[$cK] = array_reverse($chunkedValues[$cK]); } $chunkedValues = array_reverse($chunkedValues); var_dump($chunkedValues); Quote Link to comment https://forums.phpfreaks.com/topic/258750-array-output-order-in-the-index-structure/#findComment-1326470 Share on other sites More sharing options...
xyph Posted March 12, 2012 Share Posted March 12, 2012 This? <?php $nArr = array('A', 'B', 'C', 'D', 'E', 'F'); $counter = 3; $chunkedValues = array(); for( $i = 0; $i < 3; $i++ ) { $chunkedValues[$i] = array(1); for ($j = $counter-$i, $k = 0; $j > 0; $j--, $k++) { $chunkedValues[$i][$k] = $nArr[$k]; } $nArr = array_slice($nArr, $counter-$i); } var_dump($chunkedValues); ?> Quote Link to comment https://forums.phpfreaks.com/topic/258750-array-output-order-in-the-index-structure/#findComment-1326478 Share on other sites More sharing options...
felito Posted March 12, 2012 Author Share Posted March 12, 2012 thanks to all. The main problem is that the order of the index is not the natural. array 0 => array 0 => int 1 1 => string 'A' (length=1) 2 => string 'B' (length=1) 3 => string 'C' (length=1) 1 => array 1 => int 1 2 => string 'D' (length=1) 3 => string 'E' (length=1) 2 => array 2 => int 1 3 => string 'F' (length=1) is different of: array 0 => array 0 => string 'A' (length=1) 1 => string 'B' (length=1) 2 => string 'C' (length=1) 1 => array 0 => string 'D' (length=1) 1 => string 'E' (length=1) 2 => array 0 => string 'F' (length=1) this solves the issue, but i have problems with the ceil in some cases. $nArr = array('A', 'B', 'C', 'D', 'E', 'F'); $lp = 3; for ($i = 0; $i < $lp; $i++) { $m[$i][$i] = 1; for ($x = $i; $x < $lp; $x++) { $v = $i+ceil($i/2); $m[$i][$x+1] = $nArr[$x+$v]; } } Quote Link to comment https://forums.phpfreaks.com/topic/258750-array-output-order-in-the-index-structure/#findComment-1326482 Share on other sites More sharing options...
xyph Posted March 12, 2012 Share Posted March 12, 2012 That's a fairly easy change <?php $nArr = array('A', 'B', 'C', 'D', 'E', 'F'); $counter = 3; $chunkedValues = array(); for( $i = 0; $i < 3; $i++ ) { $chunkedValues[$i][$i] = 1; for ($j = $counter-$i, $k = 0; $j > 0; $j--, $k++) { $chunkedValues[$i][$k+$i+1] = $nArr[$k]; } $nArr = array_slice($nArr, $counter-$i); } var_dump($chunkedValues); ?> Sorry I didn't notice that before Quote Link to comment https://forums.phpfreaks.com/topic/258750-array-output-order-in-the-index-structure/#findComment-1326507 Share on other sites More sharing options...
felito Posted March 12, 2012 Author Share Posted March 12, 2012 thanks man, Works perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/258750-array-output-order-in-the-index-structure/#findComment-1326509 Share on other sites More sharing options...
xyph Posted March 12, 2012 Share Posted March 12, 2012 for( $i = 0; $i < 3; $i++ ) { could be for( $i = 0; $i < $counter; $i++ ) {. I neglected to do this before because you were modifying $counter in your loop. Since it's no longer modified, we can use it in the conditional. Quote Link to comment https://forums.phpfreaks.com/topic/258750-array-output-order-in-the-index-structure/#findComment-1326529 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.