Skip to content

Commit

Permalink
fix 修改ArrayHelper::searchItem/searchMutil支持可迭代的对象
Browse files Browse the repository at this point in the history
  • Loading branch information
kakuilan committed Dec 19, 2020
1 parent 7fb29ec commit ff17714
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Kph\Helpers;

use ArrayIterator;
use IteratorAggregate;

/**
* Class ArrayHelper
Expand Down Expand Up @@ -268,13 +270,15 @@ public static function combinationFull(array $arr, string $separator = '', bool

/**
* 从数组中搜索对应元素(单个).若匹配,返回该元素;否则返回false.
* @param array $arr 要搜索的数据数组
* @param array|ArrayIterator|IteratorAggregate $arr 要搜索的数据数组
* @param array $conditions 条件数组
* @param bool $delSource 若匹配,是否删除原数组的该元素
* @return bool|mixed
*/
public static function searchItem(array &$arr, array $conditions, bool $delSource = false) {
if (empty($arr) || empty($conditions)) {
public static function searchItem(&$arr, array $conditions, bool $delSource = false) {
if (!is_array($arr) && !($arr instanceof ArrayIterator) && !($arr instanceof IteratorAggregate)) {
return false;
} elseif (empty($arr) || empty($conditions)) {
return false;
}

Expand Down Expand Up @@ -306,14 +310,16 @@ public static function searchItem(array &$arr, array $conditions, bool $delSourc

/**
* 从数组中搜索对应元素(多个).若匹配,返回新数组,包含一个以上元素;否则返回空数组.
* @param array $arr 要搜索的数据数组
* @param array|ArrayIterator|IteratorAggregate $arr 要搜索的数据数组
* @param array $conditions 条件数组
* @param bool $delSource 若匹配,是否删除原数组的该元素
* @return array
*/
public static function searchMutil(array &$arr, array $conditions, bool $delSource = false): array {
public static function searchMutil(&$arr, array $conditions, bool $delSource = false): array {
$res = [];
if (empty($arr) || empty($conditions)) {
if (!is_array($arr) && !($arr instanceof ArrayIterator) && !($arr instanceof IteratorAggregate)) {
return $res;
} elseif (empty($arr) || empty($conditions)) {
return $res;
}

Expand Down

0 comments on commit ff17714

Please sign in to comment.