首页| 论坛| 消息
主题:php 在数组中有二个字符串,不在数组中
北斗星发表于 2025-12-11 19:22
$array = ['apple', 'banana', 'cherry'];
$strings = ['apple', 'date']; // 检查的字符串数组,'date'不在原数组中
$intersection = array_intersect($array, $strings); // 获取交集数组
if (count($intersection) == count($strings)) { // 如果交集长度等于原检查数组长度,则所有元素都在原数组中
echo "所有检查的字符串都存在于数组中。\n";
} else {
echo "不是所有检查的字符串都存在于数组中。\n";
}

$array = ['apple', 'banana', 'cherry'];
$strings = ['apple', 'date']; // 检查的字符串数组
$flipped = array_flip($array); // 翻转数组,使元素成为键名
foreach ($strings as $string) {
if (isset($flipped[$string])) {
echo "$string 存在于数组中。\n";
} else {
echo "$string 不存在于数组中。\n";
}
}

$array = ['apple', 'banana', 'cherry'];
$string1 = 'apple';
$string2 = 'date';
if (array_search($string1, $array) !== false) {
echo "$string1 存在于数组中。\n";
} else {
echo "$string1 不存在于数组中。\n";
}
if (array_search($string2, $array) !== false) {
echo "$string2 存在于数组中。\n";
} else {
echo "$string2 不存在于数组中。\n";
}
回帖(3):
3楼:function checkWuXing($element1, $element2) {
$xiangsheng = ['金' => '水 ..
2楼:$strings = ['apple', 'banana', 'orange', 'grape'];
$to ..
1楼:$array = ['apple', 'banana', 'orange', 'grape'];
$sear ..

全部回帖(3)»
最新回帖
收藏本帖
发新帖