EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Array Search

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Array Search

PHP-Array-Search

Introduction to PHP Array Search

A web application cannot be completed without using some set of available tools and the technologies of any specific programming language. Features like dealing with the arrays are related functionalities are very common for any web application, searching is one of them. Yes, you read it right, the array search is one of the common functionality of the array when we search an element into a defined array. An element of the array can be searched in various ways. We can also search for an array inside a multi-dimensional array. There are many ways we can go for searching functionality with the array and its elements. We can take built-in functions like array_search(), in_array(), etc into the action to get our job as per our business requirements.

Syntax of PHP Array Search

Syntax of php array search are given below:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

array_search()
array_search(element_value, array)

This function can be used to search an array value into an array and return the key (position) of that element. There are only two parameters we can see the first one is the element value and the second one is the array itself.

array_search(element_value, array, strict)

Other things remain the same as we discussed in the above syntax expect we have one additional parameter here. The parameter strict is all about using an additional check if the element value belongs to the same data type. In other words, if we go for TRUE in place of strict then the number 10 will not be equal to the “10” (string 10). Strict can carry either true or false, this is an optional parameter. By default the strict will be considered as false.

in_array()
in_array(search_value, array);

This functional will check if any specific search value exists inside an array or not. The return type of this function will be Boolean.

in_array(search, array, type)

The explanation of this function remains the same as the earlier one. It carries an extra parameter that is for the strictness like we see in the array_search().

How does PHP Array Search Work?

Before getting the searching job done for us we need to have at least two things an array first then the element value that needs to be searched in the given array.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,731 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

We can use the below-mentioned function as per our business requirement:

array_search()
in_array()

We can also use the various variant of these functions to get the job done, we will see the functionality of theses into the example area. The array search operation can be performed on the multiple arrays as well, for this we can also go for writing our own custom code.

Examples of PHP Array Search

Following are the examples of php array search are:

Example #1

Search an array element in a given.

Code:

<?php
$array = array('value -1', 'value 2', 'value 3', 'value 4', 'value 5','value -6','value -7');
$newArrays = array_search("value 5",$array); // apply array search
if($newArrays){
echo "Search found on array position: " . $newArrays; // printing the position of element if found.
}else{
echo "Nothing found.";
}
?>

Output:

PHP Array Search-1.1

Example #2

Let’s try to check how the function array_search() will work with its 3 parameters.

Code:

<?php
$array = array('value -1', 'value 2', 'value 3', 'value 4', '5','value -6','value -7');
$newArrays = array_search(5,$array,FALSE); // apply array search
echo "<b>With false</b><br/>";
if($newArrays){
echo "Search found on array position: " . $newArrays; // printing the position of element if found.
}else{
echo "Nothing found.";
}
echo "<br/><b>With True</b><br/>";
$newArrays2 = array_search(5,$array,TRUE); // apply array search
if($newArrays2){
echo "Search found on array position: " . $newArrays2; // printing the position of element if found.
}else{
echo "Nothing found.";
}
?>

Look at the value 5, 4th element of the array in the above program. We are trying to search by using array_search() with TRUE or FALSE as a third parameter.

Output:

PHP Array Search-1.2

Example #3

Let’s try to split the array and assigned to predefined array.

Code:

<?php
$array = array('value -1', 'value 2', 'value 3', 'value 4', 'value 5','value -6','value -7');
$newArrays = in_array("value 5",$array); // apply array search
if($newArrays){
echo "Search found."; // printing the position of element if found.
}else{
echo "Nothing found.";
}
?>

Output:

Output-1.3

Example #4

Array Search with an associate array.

Yes, these searches can use the search the element of an array. We also have a built-in function in PHP which will check for if a defined key exists in an array or not.

Code:

<?php
$array = array(1=> 'value -1', 11=> 'value 2', 12=>  'value 3', 15=> 'value 4', 5=> '5', 6=> 'value -6');
$newArrays = array_search(5,$array,FALSE); // apply array search
echo "<b>With false</b><br/>";
if($newArrays){
echo "Search found on position ".$newArrays; // printing the position of element if found.
}else{
echo "Nothing found.";
}
echo "<br>The use of the array_key_exists()<br>";
if (array_key_exists(12,$array))
{
echo "Key 12 exists.";
}
else
{
echo "Key 12 does not exist.";
}
?>

Output:

Output-1.4

Conclusion

There are built-in functions in the PHP language to handle the array element search in an array. array_search(), in_array(), array_key_exists() etc are one of most popular way to get the searching related job done. We need to make sure that we are using the right function as per the requirements as a significant difference among the functions can be seen as stated in various examples (mentioned above).

Recommended Articles

This is a guide to PHP Array Search. Here we also discuss the Introduction and how does php array search work? along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. PHP String Operators
  2. PHP explode()
  3. PHP strtok()
  4. PHP parse_str()

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More