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.
Check out our free courses to get an edge over the competition.
The Syntax
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:
Check out upGrad’s Advanced Certification in DevOps
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.
Example 1
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)
Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)
Example 2
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)
Explore our Popular Software Engineering Courses
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.
The Syntax
in_array (element value, array, STRICT)
Like array_search (), STRICT is an optional parameter, which is set to FALSE by default.
Example 1
<?php $arr4 = array(2,4,6,8,10);
$out4 = in_array(“10”, $arr4, false);
echo $out4; ?>
The Output – TRUE
Example 2
<?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)
In-Demand Software Development Skills
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.
Explore Our Software Development Free Courses
Conclusion
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.