What is array_search in PHP?
The array_search() in PHP is a built-in PHP function that searches for an array for a certain value and returns its appropriate key.
But if there are many values with similar characters or elements, the first matching value’s key will be returned.
Syntax Description
Now, before we implement the PHP array_search
function in our program, you have to familiarize yourself first with its correct syntax.
array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false
Familiarizing the syntax will help you avoid encountering unwanted errors that may affect the efficiency of your project and the function.
Parameters
Going further into the discussion, the PHP array_search() function takes three parameters that include the following:
$needle
$haystack
$strict
Needle
The $needle
parameter of the PHP array_search() function is a required parameter.
This is because the $needle
parameter specifies the value that the function should search for.
However, if the parameter is in the form of a string value, the function will do the comparison in a case-sensitive manner.
Haystack
The $haystack
parameter is also required by the PHP array_search() function.
This parameter specifies the original array that the function will search in.
Strict
$strict
is only an optional parameter of the function with false as its default value.
Therefore, this parameter can either be true or false which specifies the strictness of the search.
The implementation of the $strict parameter depends on the following scenarios:
- If
$strict
is set to true, the function checks for identical elements, such as the integer10
. A number10
and a string10
will be treated differently.
- However, the parameter will not be retained if it is false.
Now, before we proceed with the actual implementation of the PHP array_search() function, let us know its return values first.
Return Values:
The PHP array_search() function returns the key $needle
if it is present in the array, but if it is not, the function will return a false value.
However, if the $needle
value is found in the array more than once, the first key will be returned.
Moreover, you may use the array_keys()
function with the optional search_value
parameter instead of getting the keys for all values that match.
This time, let us have an example program to implement and test the PHP array_search() function.
PHP array_search() Example
<?php
$array = array('a', 'b', 'c', 'd');
echo array_search('d', $array). '<br>';
echo array_search('b', $array);
?>
Output:
3 1
The example shows the proper positioning and declaration of the function using its syntax given above.
It clarifies how the function works by returning the key values of the $needle
that the function searches on the $array
.
Remember that this $array
is equivalent to the definition of $haystack
which pinpoints to the array('a', 'b', 'c', 'd')
.
The PHP array_search() function then goes through the array and finds the keys of the specified array values.
After knowing the PHP array_search function, let us proceed in getting to know the PHP in_array() function.
PHP in_array() Function
The in_array() function is another built-in function of PHP that is useful for finding arrays in our program.
The PHP in_array() function is very useful for specifying whether an element is present in the given array or not.
As a result, the function returns true (boolean value) if the value exists in the array.
However, this function will return false if the value is not present in the array.
Its definition sounds similar to the PHP search_array() function, but we will see that in the examples below.
But before anything else, let us first know the following characteristics of the PHP in_array() function.
Syntax:
in_array ( $value, $array_name ,$mode )
Parameters:
As you can see in its syntax above, the PHP in_array() function takes three parameters. These parameters compose of the following:
$value
$array
$mode
Value – The value option is always necessary since it indicates the element or value within an array that the user wishes to locate.
The search value argument accepts mixed data types, including string, integer, and all other data kinds. If the user submits a string value, the array search will consider the letter case.
Array – The array parameter tells the program where to look for an element in the array. This means that this parameter must be declared every time that we implement the in_array() function.
Therefore, both the value parameter and array parameter are required by the function.
The $value
specifies the value or element to search and the $array
is the name of the array which to search the $value
from.
Mode – This argument provides the array in which the user wishes to conduct a search.
If the mode is set to TRUE, the method looks for similar values in the $array; for example, a string 10
and an integer 10
will not be deemed identical.
Therefore, the values are distinct.
However, if the mode is set to the Boolean value FALSE, strictness is not maintained, so a string of 1
is treated the same as an integer of 1
.
Return Value
The in_array() function in PHP returns a Boolean value, such as TRUE or FALSE.
If the search value is found in the specified array, this method produces the boolean value TRUE; otherwise, it returns FALSE.
This time, let us have an example program where we can test the functionality of PHP in_array, as well as see if it works similarly with the array_search function.
Example:
<?php
$sample = array('a', 'b', 'c', 'd');
$value= "a";
//find the element 10 in the array
//by default the mmode parameter is set to false
if (in_array($value, $sample))
{
//in_array() will return true and the strictness mode is set to false
echo "The element '". $value. "' exists in the array.";
}
else
{
echo "The element '". $value. "' does not exists in the array";
}
?>
Output:
The element 'a' exists in the array.
As we can see in the example program using the PHP in_array function, it has some similarities with the PHP array_serach() function.
However, the very purpose of the in_array() function is to confirm whether the element “a
” as the value of the variable $value
is present in the $sample
array.
PHP array_search, on the other hand, returns the key value of the element instead.
Now that we’ve understood two of PHP’s built-in functions, let us have a few more questions that will add up more knowledge about PHP programming.
What is the difference between in_array and array_search?
The difference between the in_array()
and array_search()
functions in PHP is that the array_search function is more precise in giving the key value of an element in an array.
The in_array() function, on the other hand, only assures the user that an element in an array is present.
However, both PHP in_array() and array_search() functions are closely similar in concept.
Specifically, they can both be used in searching and confirming if a certain element exists in a particular array.
In short, the main difference between these two functions is that array_search() usually returns either a key or an index.
While in_array() returns either TRUE or FALSE, depending on whether or not a match was found in the search.
How do you get a specific value from an array in PHP?
To get a specific value of an element from an array in PHP, we can either be specific with the index of that element or use the PHP array_search function.
This function will help us attain the desired key value or index of an element if it exists in the array.
However, if the element doesn’t exist, then the function will return a false output, indicating that the element you tried to search for does not exist.
Moreover, you can also use the in_array() functions of PHP to confirm if the element you’re looking for exists in the array.
In contrast, the function cannot give the exact key value of that element even if it is present in the array.
More About Array
- PHP Array Iterator with Example Program
- PHP Array_Keys With Example Programs
- PHP Array_Walk With Advanced Example
- PHP Arrays (With Advanced Program Examples)
Summary
This discussion involves defining the PHP array_search() function as well as its alternative function (in_array () function).
Examples are provided to support and clarify the definition of each of the functions.
Now if you have any concerns or suggestions on what we should tackle next, go on and write them in the comments below.