在 PHP 中,可以使用 in_array()
函数来判断一个值是否存在于数组中。该函数接受两个参数,第一个参数是要查找的值,第二个参数是要搜索的数组。
以下是一个示例:
$fruits = array('apple', 'banana', 'orange');
if (in_array('apple', $fruits)) {
echo 'apple exists in the array';
} else {
echo 'apple does not exist in the array';
}
输出结果为:
apple exists in the array
另外,如果你只是想判断一个数组是否包含某个值,而不需要知道具体位置,你也可以使用 array_search()
函数。该函数会返回找到的值的键名,如果没有找到则返回 false
。
以下是一个示例:
$fruits = array('apple', 'banana', 'orange');
if (array_search('apple', $fruits) !== false) {
echo 'apple exists in the array';
} else {
echo 'apple does not exist in the array';
}
输出结果与前面的示例相同。
上一篇:php 判断是否在数组中
下一篇:php将数组的值转为字符串
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站