简体   繁体   English

PHP的array_filter对面?

[英]PHP opposite of array_filter?

I'm familiar with array_filter, and I'm trying to think more functional, but I was wondering if there is a way to keep the discarded values? 我对array_filter很熟悉,并且我想尝试更多的功能,但是我想知道是否有办法保留丢弃的值? For example, if I had an array like: 例如,如果我有一个类似的数组:

<?php  
  $arr = array( 1, 2, 3, 4 );
  $arrGreaterThanTwo = array_filter($arr, function($item){
   return $item > 2;
 });

The results would be: array( 3, 4 ). 结果将是:array(3,4)。

  1. Is there a way to keep the discarded values array( 1, 2 )? 有没有办法保留丢弃的值array(1,2)?
  2. Or do I need to reuse array_filter again on the original array and return item <= to 2? 还是我需要再次在原始数组上重用array_filter并将项<=返回2?
  3. If I use array_filter again to grab array( 1, 2 ), wouldn't that be inefficient by looping over the original array twice? 如果我再次使用array_filter来抓取array(1,2),那么通过遍历原始数组两次就不会效率低下吗?

At the end, I'm just trying to loop over once with array_filter and keep the discarded values too into a separate array. 最后,我只想使用array_filter循环一次,并将丢弃的值也保存到单独的数组中。

Thanks in advance! 提前致谢!

If you want to get the other array values apart from the ones filtered, use array_diff() 如果要获取除过滤后的值以外的其他数组值,请使用array_diff()

array_diff — Computes the difference of arrays array_diff —计算数组的差

$arrRemaining = array_diff($arr, $arrGreaterThanTwo);

Output: 输出:

Array
(
  [0] => 1
  [1] => 2
)
function array_partition(array $array, callable $fn){
    $result = [[],[]];
    foreach ( $array as $value ){
        $result[$fn($value)?0:1][]=$value;
    }
    return $result;
}
// example usage:
$res = array_partition([1,2,3,4,5,6,7,8,9,10], function($i) { return $i&1==1;});

Although this is not functional implementation it has a functional interface just like array_filter . 尽管这不是功能实现,但它具有与array_filter一样的功能接口。

Updated Response 更新的回应

function test($arrOne, $fn){
  $restArr = [];

  $filterResults = array_filter($arrOne, function($item) use (&$restArr, $fn) {
      if( $fn($item) ) return $item;
      $restArr[] = $item;
  });

  return array( $filterResults, $restArr );
}

print_r( test( array(1,2,3,4), function($item){
  return $item > 2;
} ) );

Does anyone have a problem with this? 有人对此有疑问吗?

I might recommend you write your own function for it. 我可能建议您为此编写自己的函数。 This is array_bifilter – instead of returning one array, it returns two. 这是array_bifilter –而不是返回一个数组,而是返回两个。 The first is the truthy values, the second is the falsey values. 第一个是真实值,第二个是虚假值。 And yes, it only iterates thru the input array once. 是的,它只遍历输入数组一次。

function array_bifilter(array $xs, callable $f) {
  return array_reduce($xs, function ($acc, $x) use ($f) {
    if (call_user_func($f, $x))
      array_push($acc[0], $x);
    else
      array_push($acc[1], $x);
    return $acc;
  }, [[], []]);
}

list($t, $f) = array_bifilter([1,2,3,4], function($x) { return $x > 2; });

echo json_encode(['true' => $t, 'false' => $f], JSON_PRETTY_PRINT), PHP_EOL;

Output (in JSON format for easy reading) 输出(JSON格式,易于阅读)

{
    "true": [
        3,
        4
    ],
    "false": [
        1,
        2
    ]
}

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

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