Skip to content

Commit

Permalink
add inArrayAny 和 inArrayAll
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho authored Jul 12, 2018
1 parent 7ef8f39 commit 2b1faea
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function arrayShift(array $array)

/**
* 数组模糊搜索
* https://stackoverflow.com/questions/5808923
* @link https://stackoverflow.com/questions/5808923
* @param $str string 字符串
* @param $data array 数据源
* @return array
Expand All @@ -36,4 +36,32 @@ public static function arraySearch($str = '', $data)
$input = preg_quote($str, '~'); // don't forget to quote input string!
return preg_grep('~' . $input . '~', $data);
}

/**
* 检查是否存在 needles 任意一个值
* echo inArrayAny( [3,9], [5,8,3,1,2] ); // true, since 3 is present
* echo inArrayAny( [4,9], [5,8,3,1,2] ); // false, neither 4 nor 9 is present
* @link https://stackoverflow.com/questions/7542694/in-array-multiple-values
* @param $needles array
* @param $haystack array
* @return bool
*/
public static function inArrayAny($needles, $haystack)
{
return !!array_intersect($needles, $haystack);
}

/**
* 检查是否存在所有 needles
* echo inArrayAll( [3,2,5], [5,8,3,1,2] ); // true, all 3, 2, 5 present
* echo inArrayAll( [3,2,5,9], [5,8,3,1,2] ); // false, since 9 is not present
* @link https://stackoverflow.com/questions/7542694/in-array-multiple-values
* @param $needles
* @param $haystack
* @return bool
*/
public static function inArrayAll($needles, $haystack)
{
return !array_diff($needles, $haystack);
}
}

0 comments on commit 2b1faea

Please sign in to comment.