zgkhoo Posted November 13, 2007 Share Posted November 13, 2007 i wanna sort the number in an array ..which consist of 1 2 3 4 5 6 7 8 become this(split the middle of the back part of array and put into the bottom) 1 2 3 4 5 6 7 8 then become (split middle again ) 1 2 5 6 3 4 7 8 then become 1 5 3 7 2 6 4 8 then finally the array sort from 1 2 3 4 5 6 7 8 to 1 5 3 7 2 6 4 8 anyone how to write it? ??? move the back middle part of the array to Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/ Share on other sites More sharing options...
zgkhoo Posted November 13, 2007 Author Share Posted November 13, 2007 the total number is binary ..which will only 2 number 4 number 8 number (example of above is this) 16 number 32 number 64 number and so on.. Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-390698 Share on other sites More sharing options...
rajivgonsalves Posted November 13, 2007 Share Posted November 13, 2007 here you go <?php global $arrReturn; $arrReturn = array(); $arrInput = array(0=>array(1, 2, 3, 4, 5, 6, 7,); function fnSplitArray($arrInput,$intCount) { Global $arrReturn; $arrOutput = array(); $arrOne = array(); $arrTwo = array(); for ($i=0;$i<count($arrInput);$i++) { $arrOne[] = array_splice($arrInput[$i],0,count($arrInput[$i])/2); $arrTwo[] = $arrInput[$i]; } $arrOutput = array_merge($arrOne,$arrTwo); $arrReturn = $arrOutput; if ($intCount>count($arrOutput)) { fnSplitArray($arrOutput,$intCount); } } fnSplitArray($arrInput,count($arrInput[0])); print_r($arrReturn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-390776 Share on other sites More sharing options...
rajivgonsalves Posted November 13, 2007 Share Posted November 13, 2007 a little modified global $arrReturn; $arrReturn = array(); $arrInput = array(0=>array(1, 2, 3, 4, 5, 6, 7,); function fnSplitArray($arrInput,$intCount) { Global $arrReturn; $arrOutput = array(); $arrOne = array(); $arrTwo = array(); for ($i=0;$i<count($arrInput);$i++) { $arrOne[] = array_splice($arrInput[$i],0,count($arrInput[$i])/2); $arrTwo[] = $arrInput[$i]; } $arrOutput = array_merge($arrOne,$arrTwo); $arrReturn = $arrOutput; if ($intCount>count($arrOutput)) { fnSplitArray($arrOutput,$intCount); } else { $arrReturn = array(); foreach ($arrOutput as $strKey => $arrValues) { array_push($arrReturn,$arrValues[0]); } } } fnSplitArray($arrInput,count($arrInput[0])); print_r($arrReturn); Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-390785 Share on other sites More sharing options...
sasa Posted November 13, 2007 Share Posted November 13, 2007 try <?php function new_order($a){ $out = array(); $n = count($a) / 2; foreach ($a as $k => $v){ $m = $n; $newk = 0; while ($k > 0){ $newk += ($k % 2) * $m; $k = (int) $k / 2; $m = $m / 2; } $out[$newk] = $v; } ksort($out); return $out; } $a = array(1,2,3,4,5,6,7,; $b = new_order($a); print_r($b); ?> Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-390801 Share on other sites More sharing options...
zgkhoo Posted November 14, 2007 Author Share Posted November 14, 2007 thank you very much wat if i enter no enough number ? eg..only 6 number 1,2,3,4,5,6,7 wat will happen? Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-391121 Share on other sites More sharing options...
AndyB Posted November 14, 2007 Share Posted November 14, 2007 wat if ... wat will happen? Testing the script yourself will show you exactly what happens. Let us know. Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-391128 Share on other sites More sharing options...
zgkhoo Posted November 14, 2007 Author Share Posted November 14, 2007 Array ( [0] => Array ( ) [1] => Array ( [0] => 16 ) [2] => Array ( [0] => 8 ) [3] => Array ( [0] => 24 ) [4] => Array ( [0] => 4 ) [5] => Array ( [0] => 20 ) [6] => Array ( [0] => 12 ) [7] => Array ( [0] => 28 ) [8] => Array ( [0] => 2 ) [9] => Array ( [0] => 18 ) [10] => Array ( [0] => 10 ) [11] => Array ( [0] => 26 ) [12] => Array ( [0] => 6 ) [13] => Array ( [0] => 22 ) [14] => Array ( [0] => 14 ) [15] => Array ( [0] => 30 ) [16] => Array ( [0] => 1 ) [17] => Array ( [0] => 17 ) [18] => Array ( [0] => 9 ) [19] => Array ( [0] => 25 ) [20] => Array ( [0] => 5 ) [21] => Array ( [0] => 21 ) [22] => Array ( [0] => 13 ) [23] => Array ( [0] => 29 ) [24] => Array ( [0] => 3 ) [25] => Array ( [0] => 19 ) [26] => Array ( [0] => 11 ) [27] => Array ( [0] => 27 ) [28] => Array ( [0] => 7 ) [29] => Array ( [0] => 23 ) [30] => Array ( [0] => 15 ) [31] => Array ( [0] => 31 ) ) output if enter only 31 number Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-391136 Share on other sites More sharing options...
zgkhoo Posted November 14, 2007 Author Share Posted November 14, 2007 a little modified global $arrReturn; $arrReturn = array(); $arrInput = array(0=>array(1, 2, 3, 4, 5, 6, 7,); function fnSplitArray($arrInput,$intCount) { Global $arrReturn; $arrOutput = array(); $arrOne = array(); $arrTwo = array(); for ($i=0;$i<count($arrInput);$i++) { $arrOne[] = array_splice($arrInput[$i],0,count($arrInput[$i])/2); $arrTwo[] = $arrInput[$i]; } $arrOutput = array_merge($arrOne,$arrTwo); $arrReturn = $arrOutput; if ($intCount>count($arrOutput)) { fnSplitArray($arrOutput,$intCount); } else { $arrReturn = array(); foreach ($arrOutput as $strKey => $arrValues) { array_push($arrReturn,$arrValues[0]); } } } fnSplitArray($arrInput,count($arrInput[0])); print_r($arrReturn); why modified??? Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-391147 Share on other sites More sharing options...
rajivgonsalves Posted November 14, 2007 Share Posted November 14, 2007 for the array to output properly(single dimention) not as a multidimentional array Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-391167 Share on other sites More sharing options...
zgkhoo Posted November 14, 2007 Author Share Posted November 14, 2007 how to let print_r($array) in a proper way? which this ...very unreadable Array ( [0] => Array ( ) [1] => Array ( [0] => 16 ) [2] => Array ( [0] => 8 ) [3] => Array ( [0] => 24 ) [4] => Array ( [0] => 4 ) [5] => Array ( [0] => 20 ) [6] => Array ( [0] => 12 ) [7] => Array ( [0] => 28 ) [8] => Array ( [0] => 2 ) [9] => Array ( [0] => 18 ) [10] => Array ( [0] => 10 ) [11] => Array ( [0] => 26 ) [12] => Array ( [0] => 6 ) [13] => Array ( [0] => 22 ) [14] => Array ( [0] => 14 ) [15] => Array ( [0] => 30 ) [16] => Array ( [0] => 1 ) [17] => Array ( [0] => 17 ) [18] => Array ( [0] => 9 ) [19] => Array ( [0] => 25 ) [20] => Array ( [0] => 5 ) [21] => Array ( [0] => 21 ) [22] => Array ( [0] => 13 ) [23] => Array ( [0] => 29 ) [24] => Array ( [0] => 3 ) [25] => Array ( [0] => 19 ) [26] => Array ( [0] => 11 ) [27] => Array ( [0] => 27 ) [28] => Array ( [0] => 7 ) [29] => Array ( [0] => 23 ) [30] => Array ( [0] => 15 ) [31] => Array ( [0] => 31 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-391197 Share on other sites More sharing options...
rajivgonsalves Posted November 14, 2007 Share Posted November 14, 2007 like this print "<PRE>"; print_r($arr); print "</PRE>"; Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-391201 Share on other sites More sharing options...
zgkhoo Posted November 14, 2007 Author Share Posted November 14, 2007 global $arrReturn; <--wat is this use for? thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-391202 Share on other sites More sharing options...
sasa Posted November 14, 2007 Share Posted November 14, 2007 thank you very much wat if i enter no enough number ? eg..only 6 number 1,2,3,4,5,6,7 wat will happen? what you want to script do? but you say: the total number is binary ..which will only 2 number 4 number 8 number (example of above is this) 16 number 32 number 64 number and so on.. Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-391270 Share on other sites More sharing options...
rajivgonsalves Posted November 14, 2007 Share Posted November 14, 2007 That is a global variable so that its accessible because the function is call recursively Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-391274 Share on other sites More sharing options...
zgkhoo Posted November 15, 2007 Author Share Posted November 15, 2007 thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/77167-solved-very-expert-an-tough-question/#findComment-391974 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.