74

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:

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
hairynuggets
  • 3,191
  • 22
  • 55
  • 90

8 Answers8

165

Use the in_array() function.

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

if (in_array('kitchen', $array)) {
    echo 'this array contains kitchen';
}
Wiseguy
  • 20,522
  • 8
  • 65
  • 81
  • Note: It is not necessary that all the elements of a PHP array should be of same data type. They can be of different data types. If you want to match the data type too then pass `true` as the third argument of `in_array`. – Omar Tariq Mar 21 '17 at 04:25
  • how to do it with out using in_array() function? – user2215270 Jun 28 '17 at 13:25
  • @user2215270 You could iterate through the array with a [`foreach`](http://php.net/foreach) or other looping control structure, checking the current element every iteration. But why would you prefer to do that? – Wiseguy Jun 28 '17 at 14:07
22
// 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
Peter
  • 8,776
  • 6
  • 62
  • 95
12

See 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));
    }
?>
Abbas
  • 6,720
  • 4
  • 35
  • 49
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://php.net/manual/en/function.array-search.php

bobek
  • 8,003
  • 8
  • 39
  • 75
3

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

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

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

crenate
  • 3,370
  • 2
  • 20
  • 13
1

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://stackoverflow.com/a/30555568/8661779

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

Pranav Rana
  • 371
  • 2
  • 6
1
if (in_array('kitchen', $rooms) ...
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
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";
}
antelove
  • 3,216
  • 26
  • 20