An array is one of the most commonly used data structures in programming. It can be used to store different types of elements – integer, strings, other arrays. And one of the most frequent operations needed to be performed on an array is searching, more specifically, searching for a particular element of the array, finding its position in the array, etc.
PHP is one of the most prevalent and widely adopted scripting languages out there (Zuckerberg coded Facebook in PHP), and it handles array search quite elegantly. You could use a loop to run through each element, but that is cumbersome and brute-force. Instead, there are multiple built-in methods we can take advantage of like in_array(), array_search(), array_keys(), and array_key_exists(). In this blog, we are going to take a look under the hood of PHP array search.
The syntax of PHP array search is quite straightforward and easy to remember:
1. array_search (Element Value, Array, STRICT)
As you can see, you need to pass 3 parameters to the PHP array search function:
element value – This is the value that needs to be searched in the array
array – Here, we specify the array which needs to be searched
STRICT – This is an optional parameter, which identifies only exact matches. It’s a binary parameter, which can be either TRUE or FALSE. By default, it is set to FALSE. If TRUE, it checks the data type (differentiating between integer 5 and string “5”) and returns the position of the element (key) with the matching data type.
You can also choose not to specify the STRICT parameter, in which case, you will need to write the function as:
array_search (element value, array)
If no element with a matching value is found, the function returns FALSE. Alternatively, if more than one element with matching values are found, it returns the position of the first matching element.
Let us first look at PHP array search in action without the STRICT parameter.
<?php $arr1 = array(‘vinod’, ‘manish’, ‘sujay’, ‘vinit’, ‘aishwariya’);
$out1 = array_search( ‘vinit’’ ,$arr1);
echo $out1; ?>
The output, in this case, will be 3 as the element ‘vinit’ is associated with the index 3 in the array (Note that array indices start from 0, i.e, the first element of the array is at the zeroth position)
PHP Array Search With The STRICT Parameter
Let us now look at what happens when the STRICT parameter in PHP array search is set to TRUE:
<?php $arr2 = array(2,3,5,11,13,17,19);
$out2 = array_search(“11”, $arr2, true);
echo $out2; ?>
Can you guess the output here? If your guess is a NULL output, you’re absolutely right. Notice how the element to be searched is the string 11 and not the integer 11. And since this is an array of integers containing the integer 11, the function returns false.
Conversely, let us now define STRICT to be false:
<?php $arr3 = array(2,3,5,11,13,17,19);
$out3 = array_search(“11”, $arr3, false);
echo $out3; ?>
The output in this case? 3. Because the function ignored the data type (as STRICT was set to FALSE), found the element 11 to be in the 4th position and hence returned its corresponding key (3)
2. in_array ()
This PHP array search function checks if the specified element is there in the array or not. It returns a BOOLEAN, meaning if the element is found, it returns TRUE, else it returns FALSE.
in_array (element value, array, STRICT)
Like array_search (), STRICT is an optional parameter, which is set to FALSE by default.
<?php $arr4 = array(2,4,6,8,10);
$out4 = in_array(“10”, $arr4, false);
echo $out4; ?>
The Output – TRUE
<?php $arr5 = array(2,4,6,8,10);
$out5 = in_array(“10”, $arr5, true);
echo $out5; ?>
The Output – FALSE (As the function found integer 10 in the array and not the string 10 which was passed)
Learn Software Engineering Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
If you want to become proficient in PHP and make a lucrative career out of it, look no further than upGrad’s Bachelors / Master of Computer Applications courses. Affiliated with the prestigious Chandigarh University (UGC recognised), the programs offer you deep industry knowledge and a 6-month internship to boot, apart from placement assistance. You can also explore other Computer Science courses from the Software & Technology track and take your programming skills to the next level.
If you’re interested to learn more about PHP, full-stack software development, check out upGrad & IIIT-B’s Executive PG Program in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.
What is array_search in php?
An easy way to search an array for a specific value is using array_search() function. You can use array_search() with an index as the first argument, or without an index as the second argument. array_search() returns the key of the first instance of value in an array, or FALSE if it could not be found. array_search is a built-in function in PHP which accepts a value and an array and returns the index of the first occurrence of the value in the array.
What are arrays in PHP?
Arrays are used in PHP to hold multiple values. An array is actually a group of variables of the same data type. The variables in an array are always indexed by an integer key that you specify when you declare the array. The number of variables in an array must be known before it can be declared. PHP provides four data types that can be used to construct arrays: strings, integers, floats, and the special type ``. Arrays are used to store multiple values in a single variable. It is one of the most powerful features in PHP as it makes coding faster and helps in writing cleaner code. It is a collection of variables of same data type. They have no specific order of position. Arrays are used to store data in an efficient way.
What are sorting an array in PHP?
Arrays are the most important and widely-used basic data structure in PHP. They let you store multiple values in one variable. There are many interesting and useful operations available for us to manipulate arrays. Here are some basic array operations in PHP: Sorting: Arrays can be sorted, in order, based on any of the array’s properties. This is done using the sort() function. Here is an example: $movieStars = array('Tom Cruise', 'Angelina Jolie', 'John Travolta'); sort($movieStars); If you run this example, the array will be sorted alphabetically, based on the array keys. You should know that the sort() function just sorts the array’s values, but it does not change the original array; you need to do that separately.