Can I sort an array odd/even/odd/even ?is it possible via usort function? :
first array:
$array = ['a'=>'0','b'=>0,'c'=>0,'d'=1,'e'=>1,'f'=>1];
second array:
$result = ['d'=>1,'a'=>0,'e'=>1,'b'=>0,'f'=>1,'c'=>0];
Thank you.
Can I sort an array odd/even/odd/even ?is it possible via usort function? :
first array:
$array = ['a'=>'0','b'=>0,'c'=>0,'d'=1,'e'=>1,'f'=>1];
second array:
$result = ['d'=>1,'a'=>0,'e'=>1,'b'=>0,'f'=>1,'c'=>0];
Thank you.
Here's my take..
// example array
$array = array('d'=>1,'a'=>0,'b'=>0,'c'=>0,'e'=>1,'f'=>1,'g'=>1,'h'=>1);
$temp=array();
$result=array();
foreach ($array as $k=>$v) {
if ($v%2==0)
$temp[0][$k]=$v;
else
$temp[1][$k]=$v;
}
$c = max(count($temp[0]),count($temp[1]));
for ($i=0;$i<$c;$i++) {
$ok = key($temp[1]);
if ($ok) {
$result+=array($ok=>$temp[1][$ok]);
next($temp[1]);
$ok=false;
}
$ek = key($temp[0]);
if ($ek) {
$ek = key($temp[0]);
$result+=array($ek=>$temp[0][$ek]);
next($temp[0]);
$ek=false;
}
}
print_r($result);
// Array ( [d] => 1 [a] => 0 [e] => 1 [b] => 0 [f] => 1 [c] => 0 [g] => 1 [h] => 1 )