Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconWhat is Array Search PHP? & It’s Stntax

What is Array Search PHP? & It’s Stntax

Last updated:
20th May, 2021
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
What is Array Search PHP? & It’s Stntax

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:

Ads of upGrad blog

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

Ads of upGrad blog

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.

Read our Popular Articles related to Software Development

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1What 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.

2What 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.

3What 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.

Explore Free Courses

Suggested Blogs

Top 7 Node js Project Ideas &#038; Topics
31584
Node.JS is a part of the famous MEAN stack used for web development purposes. An open-sourced server environment, Node is written on JavaScript and he
Read More

by Rohan Vats

05 Mar 2024

How to Rename Column Name in SQL
46947
Introduction We are surrounded by Data. We used to store information on paper in enormous file organizers. But eventually, we have come to store it o
Read More

by Rohan Vats

04 Mar 2024

Android Developer Salary in India in 2024 [For Freshers &#038; Experienced]
901335
Wondering what is the range of Android Developer Salary in India? Software engineering is one of the most sought after courses in India. It is a reno
Read More

by Rohan Vats

04 Mar 2024

7 Top Django Projects on Github [For Beginners &amp; Experienced]
52129
One of the best ways to learn a skill is to use it, and what better way to do this than to work on projects? So in this article, we’re sharing t
Read More

by Rohan Vats

04 Mar 2024

Salesforce Developer Salary in India in 2024 [For Freshers &#038; Experienced]
909211
Wondering what is the range of salesforce salary in India? Businesses thrive because of customers. It does not matter whether the operations are B2B
Read More

by Rohan Vats

04 Mar 2024

15 Must-Know Spring MVC Interview Questions
34763
Spring has become one of the most used Java frameworks for the development of web-applications. All the new Java applications are by default using Spr
Read More

by Arjun Mathur

04 Mar 2024

Front End Developer Salary in India in 2023 [For Freshers &#038; Experienced]
902394
Wondering what is the range of front end developer salary in India? Do you know what front end developers do and the salary they earn? Do you know wh
Read More

by Rohan Vats

04 Mar 2024

Method Overloading in Java [With Examples]
26267
Java is a versatile language that follows the concepts of Object-Oriented Programming. Many features of object-oriented programming make the code modu
Read More

by Rohan Vats

27 Feb 2024

50 Most Asked Javascript Interview Questions &#038; Answers [2024]
4394
Javascript Interview Question and Answers In this article, we have compiled the most frequently asked JavaScript Interview Questions. These questions
Read More

by Kechit Goyal

26 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon