PHP and MySQL (56 Blogs) Become a Certified Professional

All you need to know about Array Search in PHP

Published on Aug 14,2019 5.4K Views


Being one of the best languages for Scripting, PHP has certainly made good use of arrays as well. So, in this article, we will understand Array Search in PHPin the following sequence:

One of the ways to search for a value in PHP is to use a loop to check the value of each element but that is inefficient. There are various inbuilt functions which could be used for searching arrays like array_search, in_array, array_keys, and array_key_exists. In this blog, we are going to discuss regarding array_search in PHP

Introduction to PHP - Array Search in PHP - Edureka

Moving on with this article on Array search in PHP.

 

Array Search

array_search is an inbuilt function in PHP. In order to search a particular value in an array, we use this function which searches for a particular value and returns the key. If there is no match found, it returns false. It is almost similar to in_array(). The main difference between both the functions is that array_search() usually returns either key or index whereas in_array() returns TRUE or FALSE according to match found in search.

Syntax: array_search(value,array,strict)

Value: It specifies the value that needs to be searched in an array.
Array: It specifies the array which needs to be searched
Strict: It is an optional parameter which searches for strictly identical elements in the array which can either be set to TRUE or FALSE. By default, it is set to FALSE. If it is set to true, it checks for identical elements. i.e integer 3 is not the same as the string 3.

When we pass the parameters,(search value & array) to the array_search(), it returns the key with a matching value as discussed above. If there is no match found, it returns false. If there is more than one match found, it returns the first matched key.

Moving on with this article on Array search in PHP

Output 1:

Let’s see an example without using the strict parameter,

<?php $var=array('ashok','hanish','aravind','naveen','manikanta'); 
$val=array_search('aravind',$var); 
echo $val; ?>

Moving on with this article on Array search in PHP

Output: 2

It returns 2 because aravind is found in the second position of the array.

In case, there is more than one match found,

<?php $var=array('ashok','hanish','aravind','naveen','manikanta','naveen'); 
$val=array_search('naveen',$var); 
echo $val; ?>

Moving on with this article on Array search in PHP

Output: 3

It returns 3 as the first match of naveen is found in the third index.

Let’s see another example using strict parameter,

<?php $var=array(1,2,3,4,11,22,33,44); 
$val=array_search("11",$var,true); 
echo $val; ?>

Moving on with this article on Array search in PHP

Output 4:

It returns with no output because data type of the value in the array and data type of the searched value is not of same type. If it is set to false, it ignores the data type and by default, it is set to false.

Let’s see the same example by setting strict parameter to false.

<?php $var=array(1,2,3,4,11,22,33,44); 
$val=array_search("11",$var,false); 
echo $val; ?>

 With this we come to an end of this article, I hope you understood the inbuilt function array_search in PHP.

If you found this blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”Array Search in PHP” and I will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

All you need to know about Array Search in PHP

edureka.co