Doyley Posted November 2, 2007 Share Posted November 2, 2007 Hi all, I have an array of numbers which will always have an even number of keys. I would like to split this array in two so there are two arrays with exactly the same number of keys. The closest function I can find is array_slice which I'm not sure how to use correctly. Does anybody have their own function that they could share? Many thanks! Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 2, 2007 Share Posted November 2, 2007 here you go with some logic applied $array = array(1,2,3,4); list($array1,$array2) = array(array_slice($array,0,floor(count($array)/2)),array_slice($array,floor(count($array)/2),count($array))); print_r($array1); print_r($array2); Quote Link to comment Share on other sites More sharing options...
Doyley Posted November 2, 2007 Author Share Posted November 2, 2007 Worked like a charm, thanks very much. One more question if you don't mind. I need to make sure that the positions of the keys in the arrays are different. For example I have 2 arrays now... Array ( [0] => 2 [1] => 21 [2] => 4 [3] => 23 [4] => 29 [5] => 12 [6] => 15 [7] => Bye ) Array ( [0] => Bye [1] => Bye [2] => Bye [3] => 22 [4] => 1 [5] => 11 [6] => 14 [7] => Bye ) Key 7 on both of these arrays are both "Bye". If this is the case I need to make sure that it is shuffled or something similar to make sue there are never two "Byes" in the same key. Do you have any ideas? Thanks again! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 2, 2007 Share Posted November 2, 2007 array_diff(), array_intersect().... Quote Link to comment Share on other sites More sharing options...
Barand Posted November 2, 2007 Share Posted November 2, 2007 try <?php $arr1 = array (1,'bye',3,4,5,6,'bye'); $arr2 = array (11,12,'bye',14,15,16,'bye'); $k1 = array_keys($arr1, 'bye'); $k2 = array_keys($arr2, 'bye'); $k3 = array_intersect($k1,$k2); while ($k3 ) { shuffle($arr1); $k1 = array_keys($arr1, 'bye'); $k3 = array_intersect($k1,$k2); } echo '<pre>A1 ', print_r($arr1, true), '</pre>'; echo '<pre>A2 ', print_r($arr2, true), '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
Doyley Posted November 2, 2007 Author Share Posted November 2, 2007 Thanks for your help guys. Barand, I managed to write my own before I notice you replied but thanks anyway... If anybody is interested, here is the code. It seems to work pretty well. function splitArray($runners){ shuffle($runners); $array=$runners; list($array1,$array2) = array(array_slice($array,0,floor(count($array)/2)),array_slice($array,floor(count($array)/2),count($array))); $result[]=$array1; $result[]=$array2; return $result; } function checkNoDupe($result){ $array1=$result[0]; $array2=$result[1]; for($x=0;$x<=count($array1)-1;$x++){ if($array1[$x]==$array2[$x]){ return true; } } } function runIt($runners){ global $numbyes, $num; $result=splitArray($runners); if($numbyes<=$num){ while(checkNoDupe($result)){ $result=splitArray($runners); } } return $result; } $result=runIt($runners); 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.