在奇数/偶数数字键上拆分数组,我做错了什么


What am I doing wrong by splitting my array on odd/even numeric keys?

我在将数组一分为二时遇到问题。

Array
(
    [0] => livree
    [1] => 2011-12-26
    [2] => livree
    [3] => 2011-12-27
    [4] => livree
    [5] => 2011-12-28
    [6] => livree
    [7] => 2011-12-29
    [8] => livree
    [9] => 2011-12-30
    [10] => livree
    [11] => 2011-12-31
    [12] => livree
    [13] => 2012-01-01
    [14] => livree
    [15] => 2012-01-02
    [16] => livree
    [17] => 2012-01-03
    [18] => en_cours
    [19] => 2012-01-04
    [20] => en_cours
    [21] => 2012-01-05
    [22] => en_cours
    [23] => 2012-01-06
    [24] => en_cours
    [25] => 2012-01-07
    [26] => en_cours
    [27] => 2012-01-08
)

我使用这些函数来检测奇数/偶数密钥,并将其拆分为两个不同的数组:

function odd($var){return($var & 1);}
function even($var){return(!($var & 1));}

$odd = array_filter($vb, "odd");
$even = array_filter($vb, "even");

我只有两个阵列:

Array
(
    [0] => 2011-12-26
    [1] => 2011-12-27
    [2] => 2011-12-28
    [3] => 2011-12-29
    [4] => 2011-12-30
    [5] => 2011-12-31
    [6] => 2012-01-01
    [7] => livree
    [8] => livree
    [9] => en_cours
    [10] => en_cours
    [11] => en_cours
    [12] => en_cours
    [13] => en_cours
)

Array
(
    [0] => livree
    [1] => livree
    [2] => livree
    [3] => livree
    [4] => livree
    [5] => livree
    [6] => livree
    [7] => 2012-01-02
    [8] => 2012-01-03
    [9] => 2012-01-04
    [10] => 2012-01-05
    [11] => 2012-01-06
    [12] => 2012-01-07
    [13] => 2012-01-08
)

我做错了什么???谢谢你的帮助!

array_filter传递给您的是值,而不是键。我不明白为什么你会得到这些结果,但无论如何,你根本不需要array_filter:

更快的方法:

$odd = $even = array();
for ($i = 0, $l = count($vb); $i < $l;) { // Notice how we increment $i each time we use it below, by two in total
    $even[] = $vb[$i++];
    $odd[] = $vb[$i++];
}

更巧妙的方法:

foreach (array_chunk($vb, 2) as $chunk) {
    $even[] = $chunk[0];
    $odd[] = $chunk[1];
}

出于某种原因,我还认为你真的想要一个关联数组:

foreach (array_chunk($vb, 2) as $chunk) {
    $days[$chunk[1]] = $chunk[0];
}
for($i = 0; $i < sizeof($yourarray); $i = $i+2) {
    $even[] = $yourarray[$i];
    $odd[] = $yourarray[$i+1];
}
// See if the array is having even no. of elements for example if it would be having 28 elements instead of 27 then we will miss the 28th element in the loop. So we have to check that and add it to the even array.
if((sizeof($yourarray) % 2) == 0)
    $even[] = $yourarray[sizeof($yourarray-1)];    

就是这样!

function is_odd($num)
 {
    if ($num % 2 == 0)
        return false;
     else     
        return true;
}
$even_array=array();
$odd_rray=array(); 
foreach($array as $key=>$val)
{
    if(is_odd($key))
        array_push($odd_array,$val)
    else
        array_push($even_array,$val)
}
print_r($even_array);
print_r($odd_array);

我不擅长PHP,但以下是我在C#中(不使用LINQ)要做的事情:

object[] input = new object{
   "livee", new DateTime(2012,1,1),
   "livee", new DateTime(2012,1,2),
   "livee", new DateTime(2012,1,3),
   "livee", new DateTime(2012,1,4)};
ArrayList stringValues = new ArrayList();
ArrayList dateValues = new ArrayList();
for(int i = 0; i< input.Length; i ++)
{
  if(i % 2 == 0)
    stringValues.Add(input[i]);
  else
    dateValues.Add(input[i]);
}

在C#中使用LINQ可以实现以下功能:

string[] stringValues = input.OfType<string>().ToArray();
DateTime[] dateValues = input.OfType<DateTime>().ToArray();

或者,我们也可以这样做:

object[] stringValues = input.Where((obj, index) => index % 2 == 0).ToArray();
object[] dateValues = input.Where((obj, index) => index % 2 == 1).ToArray();

有一些PHP实现,例如http://phplinq.codeplex.com/.