From ff177149ff09c9f55c0e6d262c753d3e2c9faeb5 Mon Sep 17 00:00:00 2001 From: kakuilan Date: Sat, 19 Dec 2020 10:59:49 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E6=94=B9ArrayHelper::searchItem?= =?UTF-8?q?/searchMutil=E6=94=AF=E6=8C=81=E5=8F=AF=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E7=9A=84=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Helpers/ArrayHelper.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Helpers/ArrayHelper.php b/src/Helpers/ArrayHelper.php index 708d30c..c090548 100644 --- a/src/Helpers/ArrayHelper.php +++ b/src/Helpers/ArrayHelper.php @@ -10,6 +10,8 @@ namespace Kph\Helpers; +use ArrayIterator; +use IteratorAggregate; /** * Class ArrayHelper @@ -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; } @@ -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; }