InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Checking if an Element Is in an Array in PHP
PHP

Checking if an Element Is in an Array in PHP

InfinityCoder November 20, 2016

You want to know if an array contains a certain value.

Use in_array():

1
2
3
if (in_array($value, $array)) {
    // an element has $value as its value in array $array
}

Use in_array() to check if an element of an array holds a value:

1
2
3
4
5
6
7
8
$book_collection = array('Emma', 'Pride and Prejudice', 'Northhanger Abbey');
$book = 'Sense and Sensibility';
 
if (in_array($book, $book_collection)) {
    echo 'Own it.';
} else {
    echo 'Need it.';
}

The default behavior of in_array() is to compare items using the == operator. To use the strict equality check, ===, pass true as the third parameter to in_array():

1
2
3
4
5
6
7
8
$array = array(1, '2', 'three');
 
in_array(0, $array);        // true!
in_array(0, $array, true);  // false
in_array(1, $array);        // true
in_array(1, $array, true);  // true
in_array(2, $array);        // true
in_array(2, $array, true);  // false

The first check, in_array(0, $array), evaluates to true because to compare the number 0 against the string three, PHP casts three to an integer.

Because three isn’t a numeric string, as is 2, it becomes 0. Therefore, in_array() thinks there’s a match.
Consequently, when comparing numbers against data that may contain strings, it’s safest to use a strict comparison.
If you find yourself calling in_array() multiple times on the same array, it may be better to use an associative array, with the original array elements as the keys in the new associative array.

Looking up entries using in_array() takes linear time; with an associative array, it takes constant time.
If you can’t create the associative array directly but need to convert from a traditional one with integer keys, use array_flip() to swap the keys and values of an array:

1
2
3
4
5
6
7
8
9
10
11
12
13
$book_collection = array('Emma',
                         'Pride and Prejudice',
                         'Northhanger Abbey');
 
// convert from numeric array to associative array
$book_collection = array_flip($book_collection);
$book = 'Sense and Sensibility';
 
if (isset($book_collection[$book])) {
    echo 'Own it.';
} else {
    echo 'Need it.';
}

Note that doing this condenses multiple keys with the same value into one element in the flipped array.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Redirecting Mobile Browsers to a Mobile Optimized Site in PHP
You want to send mobile or tablet browsers to an …

Redirecting Mobile Browsers to a Mobile Optimized Site in PHP

Applying a Function to Each Element in an Array in PHP
You want to apply a function or method to each …

Applying a Function to Each Element in an Array in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap