Jump to content

[SOLVED] Split Array


Doyley

Recommended Posts

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>';

?>

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.