如何检查数组中是否包含php中的特定值?

时间:2022-07-14 21:35:52

I have a PHP variable of type Array and I would like find out if it contains a specific value and let the user know that it is there. This is my array:

我有一个PHP变量类型的数组,我想知道它是否包含一个特定值,并让用户知道它在那里。这是我的数组:

Array ( [0] => kitchen [1] => bedroom [2] => living_room [3] => dining_room) 

and I would like do something like:

我想做的是:

if(Array contains 'kitchen') {echo 'this array contains kitchen';}

What is the best way to do the above?

完成上述任务的最佳方式是什么?

8 个解决方案

#1


117  

Use the in_array() function.

使用in_array()函数。

$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');

if (in_array('kitchen', $array)) {
    echo 'this array contains kitchen';
}

#2


13  

// Once upon a time there was a farmer

// He had multiple haystacks
$haystackOne = range(1, 10);
$haystackTwo = range(11, 20);
$haystackThree = range(21, 30);

// In one of these haystacks he lost a needle
$needle = rand(1, 30);

// He wanted to know in what haystack his needle was
// And so he programmed...
if (in_array($needle, $haystackOne)) {
    echo "The needle is in haystack one";
} elseif (in_array($needle, $haystackTwo)) {
    echo "The needle is in haystack two";
} elseif (in_array($needle, $haystackThree)) {
    echo "The needle is in haystack three";
}

// The farmer now knew where to find his needle
// And he lived happily ever after

#3


9  

See in_array

看到in_array

<?php
    $arr = array(0 => "kitchen", 1 => "bedroom", 2 => "living_room", 3 => "dining_room");    
    if (in_array("kitchen", $arr))
    {
        echo sprintf("'kitchen' is in '%s'", implode(', ', $arr));
    }
?>

#4


3  

You need to use a search algorithm on your array. It depends on how large is your array, you have plenty of choices on what to use. Or you can use on of the built in functions:

您需要在数组上使用搜索算法。这取决于你的数组有多大,你有很多选择。或使用内置函数:

http://www.w3schools.com/php/php_ref_array.asp

http://www.w3schools.com/php/php_ref_array.asp

http://php.net/manual/en/function.array-search.php

http://php.net/manual/en/function.array-search.php

#5


3  

From http://php.net/manual/en/function.in-array.php

从http://php.net/manual/en/function.in-array.php

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

bool in_array(混合$指针,数组$haystack [, bool $strict = FALSE])

Searches haystack for needle using loose comparison unless strict is set.

使用宽松的比较搜索大海捞针,除非设置严格。

#6


1  

if (in_array('kitchen', $rooms) ...

#7


0  

Using dynamic variable for search in array

在数组中使用动态变量进行搜索

 /* https://ideone.com/Pfb0Ou */

$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');

/* variable search */
$search = 'living_room';

if (in_array($search, $array)) {
    echo "this array contains $search";
} else
    echo "this array NOT contains $search";

#8


0  

Following is how you can do this:

以下是如何做到这一点:

<?php
$rooms = ['kitchen', 'bedroom', 'living_room', 'dining_room']; # this is your array
if(in_array('kitchen', $rooms)){
    echo 'this array contains kitchen';
}

Make sure that you search for kitchen and not Kitchen. This function is case sensitive. So, the below function simply won't work:

确保你搜索的是厨房而不是厨房。这个函数是区分大小写的。因此,下面的函数根本不起作用:

$rooms = ['kitchen', 'bedroom', 'living_room', 'dining_room']; # this is your array
if(in_array('KITCHEN', $rooms)){
    echo 'this array contains kitchen';
}

If you rather want a quick way to make this search case insensitive, have a look at the proposed solution in this reply: https://*.com/a/30555568/8661779

如果您想要一种快速的方法来使这个搜索用例不敏感,请查看本文中建议的解决方案:https://*.com/a/30555568/8661779

Source: http://dwellupper.io/post/50/understanding-php-in-array-function-with-examples

来源:http://dwellupper.io/post/50/understanding-php-in-array-function-with-examples

#1


117  

Use the in_array() function.

使用in_array()函数。

$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');

if (in_array('kitchen', $array)) {
    echo 'this array contains kitchen';
}

#2


13  

// Once upon a time there was a farmer

// He had multiple haystacks
$haystackOne = range(1, 10);
$haystackTwo = range(11, 20);
$haystackThree = range(21, 30);

// In one of these haystacks he lost a needle
$needle = rand(1, 30);

// He wanted to know in what haystack his needle was
// And so he programmed...
if (in_array($needle, $haystackOne)) {
    echo "The needle is in haystack one";
} elseif (in_array($needle, $haystackTwo)) {
    echo "The needle is in haystack two";
} elseif (in_array($needle, $haystackThree)) {
    echo "The needle is in haystack three";
}

// The farmer now knew where to find his needle
// And he lived happily ever after

#3


9  

See in_array

看到in_array

<?php
    $arr = array(0 => "kitchen", 1 => "bedroom", 2 => "living_room", 3 => "dining_room");    
    if (in_array("kitchen", $arr))
    {
        echo sprintf("'kitchen' is in '%s'", implode(', ', $arr));
    }
?>

#4


3  

You need to use a search algorithm on your array. It depends on how large is your array, you have plenty of choices on what to use. Or you can use on of the built in functions:

您需要在数组上使用搜索算法。这取决于你的数组有多大,你有很多选择。或使用内置函数:

http://www.w3schools.com/php/php_ref_array.asp

http://www.w3schools.com/php/php_ref_array.asp

http://php.net/manual/en/function.array-search.php

http://php.net/manual/en/function.array-search.php

#5


3  

From http://php.net/manual/en/function.in-array.php

从http://php.net/manual/en/function.in-array.php

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

bool in_array(混合$指针,数组$haystack [, bool $strict = FALSE])

Searches haystack for needle using loose comparison unless strict is set.

使用宽松的比较搜索大海捞针,除非设置严格。

#6


1  

if (in_array('kitchen', $rooms) ...

#7


0  

Using dynamic variable for search in array

在数组中使用动态变量进行搜索

 /* https://ideone.com/Pfb0Ou */

$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');

/* variable search */
$search = 'living_room';

if (in_array($search, $array)) {
    echo "this array contains $search";
} else
    echo "this array NOT contains $search";

#8


0  

Following is how you can do this:

以下是如何做到这一点:

<?php
$rooms = ['kitchen', 'bedroom', 'living_room', 'dining_room']; # this is your array
if(in_array('kitchen', $rooms)){
    echo 'this array contains kitchen';
}

Make sure that you search for kitchen and not Kitchen. This function is case sensitive. So, the below function simply won't work:

确保你搜索的是厨房而不是厨房。这个函数是区分大小写的。因此,下面的函数根本不起作用:

$rooms = ['kitchen', 'bedroom', 'living_room', 'dining_room']; # this is your array
if(in_array('KITCHEN', $rooms)){
    echo 'this array contains kitchen';
}

If you rather want a quick way to make this search case insensitive, have a look at the proposed solution in this reply: https://*.com/a/30555568/8661779

如果您想要一种快速的方法来使这个搜索用例不敏感,请查看本文中建议的解决方案:https://*.com/a/30555568/8661779

Source: http://dwellupper.io/post/50/understanding-php-in-array-function-with-examples

来源:http://dwellupper.io/post/50/understanding-php-in-array-function-with-examples