PHP array search


Home - Tutorials - Basics

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

Tutorial info:


Name:PHP array search
Total steps:1
Category:Basics
Level:Beginner

Bookmark PHP array search



Bookmark and Share

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


Follow phpf1 on Twitter




F1 Site Family
AJAX F1
CSS F1
Database F1
HTML F1
Java F1
JavaScript F1
PHP F1

Total time: 0.022