这里有一个 PHP 函数,它的功能类似于 JavaScript 的
Array.some() 函数。
我相信代码本身已经很清晰了,但如果您有任何问题,请随时问我。
$array_some = function($an_array, $callback_function) {
$is_data_found = false;
foreach ($an_array as $an_array_index => $array_item) {
$is_data_found = $callback_function($array_item, $an_array_index, $an_array);
if ($is_data_found) return $is_data_found;
}
return $is_data_found;
};
在普通数组中的使用:
$numbers = [12, 34, 27, 23, 65, 93, 36, 87, 4, 254];
$is_any_number_less_than500 = $array_some($numbers, fn($number) => $number < 500) === false ? 'false' : 'true';
echo("<pre>is any number < 500: $is_any_number_less_than500</pre><br />");
$is_any_number_more_than500 = $array_some($numbers, fn($number) => $number > 500) === false ? 'false' : 'true';
echo("<pre>is any number > 500: $is_any_number_more_than500</pre><br /><br />");
在关联数组中的使用:
(类似 JavaScript 对象数组 / 类似 JavaScript JSON)
$products = [
['id' => 'id_1', 'price' => 30],
['id' => 'id_2', 'price' => 233],
['id' => 'id_3', 'price' => 5],
['id' => 'id_4', 'price' => 499]
];
$is_any_product_price_less_than500 = $array_some($products, fn($product) => @$product['price'] < 500) === false ? 'false' : 'true';
echo("<pre>is any product price < 500: $is_any_product_price_less_than500</pre><br />");
$is_any_product_price_more_than500 = $array_some($products, fn($product) => @$product['price'] > 500) === false ? 'false' : 'true';
echo("<pre>is any product price > 500: $is_any_product_price_more_than500</pre><br /><br />");
使用操作数据:
$is_any_number_bigger_than10 = $array_some([2, 5, 8, 1, 4], fn($number) => $number > 10) === false ? 'false' : 'true';
echo("<pre>is any number > 10: $is_any_number_bigger_than10</pre><br />");
$is_any_number_bigger_than10 = $array_some([12, 5, 8, 1, 4], fn($number) => $number > 10) === false ? 'false' : 'true';
echo("<pre>is any number > 10: $is_any_number_bigger_than10</pre><br /><br />");