Maison  >  Article  >  développement back-end  >  Comment déterminer si une valeur est dans un tableau en PHP (une brève analyse de diverses méthodes)

Comment déterminer si une valeur est dans un tableau en PHP (une brève analyse de diverses méthodes)

PHPz
PHPzoriginal
2023-04-12 09:19:581151parcourir

PHP是一种流行的编程语言,其数组是一种非常重要的数据结构。当我们需要判断一个值是否在数组中时,我们可以使用PHP中的in_array()函数。本文将详细介绍如何使用in_array()函数以及其他几种方法来判断值是否在数组中。

一、使用in_array()函数判断值是否在数组中

在PHP中,in_array()函数是判断一个值是否在数组中的最简单和最常用的方法。它的语法如下:

in_array($value, $array, $strict);

其中,$value是要查找的值,$array是要查找的数组,$strict是可选的参数,表示是否要进行类型检查。

例如,我们有一个数组$fruits,它包含了一些水果:

$fruits = [
  'apple',
  'banana',
  'pear',
  'orange',
  'kiwi'
];

我们现在想要判断"pear"是否在数组$fruits中,可以使用以下代码:

if (in_array('pear', $fruits)) {
  echo 'pear is in the array.';
} else {
  echo 'pear is not in the array.';
}

输出结果为:

pear is in the array.

如果要进行类型检查,可以将$strict参数设置为true。例如:

$numbers = [1, 2, 3, 4, 5];
if (in_array('1', $numbers, true)) {
  echo '1 is in the array.';
} else {
  echo '1 is not in the array.';
}

输出结果为:

1 is not in the array.

二、使用array_search()函数判断值是否在数组中

另一种判断值是否在数组中的方法是使用array_search()函数。它的语法如下:

array_search($value, $array, $strict);

其中,$value是要查找的值,$array是要查找的数组,$strict是可选的参数,表示是否要进行类型检查。如果找到了,该函数会返回该值在数组中的键名;如果没找到,返回false。

例如,我们有一个关联数组$prices,它包含了一些水果的价格:

$prices = [
  'apple' => 0.5,
  'banana' => 0.3,
  'pear' => 0.6,
  'orange' => 0.8,
  'kiwi' => 0.7
];

我们现在想要判断"banana"的价格在数组$prices中是否存在,可以使用以下代码:

$key = array_search(0.3, $prices);
if ($key !== false) {
  echo 'banana exists and the price is ' . $prices['banana'];
} else {
  echo 'banana does not exist.';
}

输出结果为:

banana exists and the price is 0.3

如果要进行类型检查,可以将$strict参数设置为true。例如:

$key = array_search('0.3', $prices, true);
if ($key !== false) {
  echo 'banana exists but its price is in a different data type.';
} else {
  echo 'banana does not exist.';
}

输出结果为:

banana does not exist.

三、使用isset()函数判断值是否在数组中

另一种简单的方法是使用isset()函数来判断一个键是否存在于数组中。例如:

if (isset($prices['banana'])) {
  echo 'banana exists and the price is ' . $prices['banana'];
} else {
  echo 'banana does not exist.';
}

输出结果与前面的例子相同,为:

banana exists and the price is 0.3

但是,这种方法无法判断一个值是否存在于数组中的值中,只能判断一个键是否存在于数组中。因此,它只适用于普通数组,不适用于关联数组或多维数组。

四、使用in_array()与array_search()的扩展方法

除了直接使用in_array()和array_search()函数外,还有一些扩展的方法可以帮助我们更好地判断值是否在数组中。例如:

1.在in_array()函数中使用array_keys()函数:

if (in_array($value, array_keys($array))) {
  echo $value . ' exists in the array.';
} else {
  echo $value . ' does not exist in the array.';
}

这种方法的原理是先使用array_keys()函数获取数组中所有的键,然后使用in_array()函数判断值是否在键的列表中。但是,这种方法效率较低,如果数组较大,会占用大量内存和时间。

2.使用array_flip()函数和isset()函数:

if (isset(array_flip($array)[$value])) {
  echo $value . ' exists in the array.';
} else {
  echo $value . ' does not exist in the array.';
}

这种方法的原理是先使用array_flip()函数将数组中的键和值进行反转,然后使用isset()函数判断值是否存在于新数组中。这种方法的效率较高,但是只适用于普通数组,不适用于关联数组或多维数组。

五、总结

在PHP中,判断一个值是否在数组中有多种方法,其中最常用的方法是使用in_array()函数和array_search()函数。如果要进行类型检查,需要将$strict参数设置为true。除此之外,还有一些扩展的方法,可以帮助我们更好地判断值是否在数组中。不同的情况下,不同的方法可以选择不同的方法。

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn