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
Finding the Position of a Value in an Array in PHP
PHP

Finding the Position of a Value in an Array in PHP

InfinityCoder November 20, 2016

You want to know if a value is in an array. If the value is in the array, you want to know its key.

Use array_search(). It returns the key of the found value. If the value is not in the array, it returns false:

1
2
3
4
$position = array_search($value, $array);
if ($position !== false) {
    // the element in position $position has $value as its value in array $array
}

Use in_array() to find if an array contains a value; use array_search() to discover where that value is located.

However, because array_search() gracefully handles searches in which the value isn’t found, it’s better to use array_search() instead of in_array().

The speed difference is minute, and the extra information is potentially useful:

1
2
3
4
5
6
7
8
9
10
$favorite_foods = array(1 => 'artichokes', 'bread', 'cauliflower',
                        'deviled eggs');
$food = 'cauliflower';
$position = array_search($food, $favorite_foods);
 
if ($position !== false) {
    echo "My #$position favorite food is $food";
} else {
    echo "Blech! I hate $food!";
}

Use the !== check against false because if your string is found in the array at position 0, the if evaluates to a logical false, which isn’t what is meant or wanted.
If a value is in the array multiple times, array_search() is only guaranteed to return one of the instances, not the first instance.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Validating XML Documents in PHP
You want to make sure your XML document abides by …

Validating XML Documents in PHP

Fetching a URL with the POST Method and Form Data in PHP
You want to submit a document using the POST method, …

Fetching a URL with the POST Method and Form Data 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