【发布时间】:2022-01-27 07:40:25
【问题描述】:
用例:
$a = [1, 2, [3, 4, 5], [6, [7, 8], 8, 10]];
isNumberPresentInArray($a, 10) // returns true;
isNumberPresentInArray($a, 2) // returns true;
isNumberPresentInArray($a, 14) // returns false;
我想检查数组中是否存在元素。以下是我的代码版本。但它不适用于内部数组。请帮帮我。
$a = [1, 2, [3, 4, 5], [6, [7, 8], 8, 10]];
function isNumberPresentInArray($a, $b) {
foreach($a as $v)
{
if (is_array($v)) {
return isNumberPresentInArray($v, $b);
} else {
if ($v == $b) {
return true;
}
}
}
}
echo isNumberPresentInArray($a, 1);
【问题讨论】: