PHP array search


Home - Tutorials - Basics

In this tutorial I will show you how to search in a PHP array.

Step 1 - Search in array


PHP array search


Somtimes you have a quite big array which is filled up from a file or code. In this case you don't know whether a given value exists in the array or not. And if it exists then it would be fine to get the key of this element.

Let's suppose you have the following array:

Code:
  1. $colorList = array("apple"=>"red",
  2. "grass"=>"green",
  3. "sky"=>"blue",
  4. "night"=>"black",
  5. "wall"=>"white");

Now if you want to know whether the value "blue" exists you can use the PHP built in function array_search(). With this you can easy get this information in the following way:
Code:
  1. echo 'Blue is '.array_search("blue",$colorList);

That's all.






Tags: php array search, search array, php, array, search

PHP array search - Table of contents
Step 1 - Search in array

Partners
AJAX F1
CSS F1
Database F1
Forex F1
Flash F1
HTML F1
JavaScript F1
PhotoShop F1
PHP F1
MaxTutorial
Monthly mortgage payment calculator
WebFormGenerator

Forex mini accounts

Total time: 0.0071