我在奇数/偶数键上分割数组有什么错?

时间:2022-09-20 19:05:57

I'm having trouble splitting an array in two.

把一个数组分成两部分有困难。

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
)

I use those functions to detect odd/even key and split it in two differents array:

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

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


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

I'm only having thoses two array:

我只有两个数组

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
)

What did I do wrong??? Thx for your help!

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

4 个解决方案

#1


6  

array_filter passes you the value, not the key. I fail to understand why you are getting exactly these results, but anyway, you don't need array_filter at all:

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

Faster way to do it:

更快的方法:

$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++];
}

Cuter way to do it:

更可爱的做法:

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

...and for some reason I also think you really wanted an associative array:

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

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

#2


1  

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

Thats it!

这就是它!

#3


0  

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

#4


-1  

I am not good with PHP, but here's what I would do in C# (and without using LINQ):

我对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]);
}

Using LINQ in C# the following is possible:

在c#中使用LINQ是可能的:

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

Alternatively, we can also do:

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

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

There are a few PHP implementations, for example at http://phplinq.codeplex.com/.

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

#1


6  

array_filter passes you the value, not the key. I fail to understand why you are getting exactly these results, but anyway, you don't need array_filter at all:

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

Faster way to do it:

更快的方法:

$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++];
}

Cuter way to do it:

更可爱的做法:

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

...and for some reason I also think you really wanted an associative array:

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

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

#2


1  

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

Thats it!

这就是它!

#3


0  

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

#4


-1  

I am not good with PHP, but here's what I would do in C# (and without using LINQ):

我对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]);
}

Using LINQ in C# the following is possible:

在c#中使用LINQ是可能的:

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

Alternatively, we can also do:

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

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

There are a few PHP implementations, for example at http://phplinq.codeplex.com/.

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