简体   繁体   English

过滤器阵列-奇偶

[英]Filter array - odd even

How can a filter out the array entries with an odd or even index number? 如何过滤出具有奇数或偶数索引号的数组项?

Array
(
    [0] => string1
    [1] => string2
    [2] => string3
    [3] => string4
)

Like, i want it remove the [0] and [2] entries from the array. 像,我希望它从数组中删除[0]和[2]条目。 Or say i have 0,1,2,3,4,5,6,7,8,9 - i would need to remove 0,2,4,6,8. 或者说我有0、1、2、3、4、5、6、7、8、9-我需要删除0、2、4、6、8。

foreach($arr as $key => $value) if($key&1) unset($arr[$key]);

The above removes odd number positions from the array, to remove even number positions, use the following: 上面的代码从数组中删除了奇数位置,要删除偶数位置,请使用以下命令:

Instead if($key&1) you can use if(!($key&1)) 可以使用if(!($key&1))代替if($key&1) if(!($key&1))

You could also use SPL FilterIterator like this: 您也可以像这样使用SPL FilterIterator:

class IndexFilter extends FilterIterator {
    public function __construct (array $data) {
        parent::__construct(new ArrayIterator($data));
    }   

    public function accept () {
        # return even keys only
        return !($this->key() % 2);
    }     
}

$arr      = array('string1', 'string2', 'string3', 'string4');
$filtered = array();

foreach (new IndexFilter($arr) as $key => $value) {
    $filtered[$key] = $value;
}

print_r($filtered);

Here's a "hax" solution: 这是一个“ hax”解决方案:

Use array_filter in combination with an "isodd" function. 结合使用array_filter和“ isodd”函数。

array_filter seems only to work on values, so you can first array_flip and then use array_filter. array_filter似乎仅适用于值,因此您可以先array_flip然后再使用array_filter。

array_flip(array_filter(array_flip($data), create_function('$a','return $a%2;')))
<?php
function odd($var)
{
    // returns whether the input integer is odd
    return($var & 1);
}

function even($var)
{
    // returns whether the input integer is even
    return(!($var & 1));
}

$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);

echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
?>

Odd :
Array
(
    [a] => 1
    [c] => 3
    [e] => 5
)
Even:
Array
(
    [0] => 6
    [2] => 8
    [4] => 10
    [6] => 12
)
<?php 
$array = array(0, 3, 5, 7, 20, 10, 99,21, 14, 23, 46); 
for ($i = 0; $i < count($array); $i++) 

{ 
        if ($array[$i]%2 !=0)
        echo $array[$i]."<br>";
}
?> 
$array = array(0 => 'string1', 1 => 'string2', 2 => 'string3', 3 => 'string4');

// Removes elements of even-numbered keys
$test1 = array_filter($array, function($key) {
    return ($key & 1);
}, ARRAY_FILTER_USE_KEY);

// Removes elements of odd-numbered-keys
$test2 = array_filter($array, function($key) {
    return !($key & 1);
}, ARRAY_FILTER_USE_KEY);

This answer is a bit of an improvement over this answer. 这个答案比这个答案有些改进。 You can use anonymous functions instead of create_function . 您可以使用匿名函数代替create_function Also, array_filter takes an optional flag parameter that lets you specify that the callback function takes the key as its only argument. 此外, array_filter带有可选的flag参数,该参数可让您指定回调函数将键作为唯一参数。 So you don't need to use array_flip to get an array of keys. 因此,您无需使用array_flip即可获取键数组。

I'd do it like this... 我会这样...

for($i = 0; $i < count($array); $i++)
{
    if($i % 2) // OR if(!($i % 2))
    {
        unset($array[$i]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM