Array Search
In PHP, array_search() function performs a search for a value in an array, as with in_array(). If the value is found then the index position of that key (which could be string or number) will be returned.
| General Format | mixed array_search ( mixed $search , array $array [, bool $strict ] ) | ||
| Parameters | $search: The value to be searched | $array: The array in which the value to be searched | $strict: If the value is set to true then the types of $search and $array will be checked |
| Return Value | Return the key for $search if it is found in $array, otherwise false | ||
Example 1:
<?php
$array=array( "new", "Delhi", "common", "wealth", "game"); echo"<br/><b>Initially the values of \$array is:</b><br/>";var_dump(
$array); $key='common'; echo"<br/><b>Now we will search the word \"$key\" in \$array</b><br/>"; $search=array_search($key,$array); echo"<br/><b>$key is found at $search</b><br/>";?>
Output:
Initially the values of $array is:
array(5) {
[0]=>
string(3) "new"
[1]=>
string(5) "Delhi"
[2]=>
string(6) "common"
[3]=>
string(6) "wealth"
[4]=>
string(4) "game"
}
Now we will search the word "common" in $array
common is found at 2
Example 2:
<?php
$array=array(10=>"new",11=>"delhi",12=>"common",13=>"wealth",14=>"game"); echo"<br/><b>Initially the values of \$array is:</b><br/>";var_dump(
$array); $key='common'; echo"<br/><b>Now we will search the word \"$key\" in \$array</b><br/>"; $search=array_search($key,$array); echo"<br/><b>$key is found at $search</b><br/>";?>
Output:
Initially the values of $array is:
array(5) {
[10]=>
string(3) "new"
[11]=>
string(5) "delhi"
[12]=>
string(6) "common"
[13]=>
string(6) "wealth"
[14]=>
string(4) "game"
}
Now we will search the word "common" in $array
common is found at 12
Example 3:
<?php
$array=array("new"=>11,"delhi"=>12,"common" => 13,"wealth"=>14,"game"=>15); echo"<br/><b>Initially the values of \$array is:</b><br/>";var_dump(
$array); $key=13; echo"<br/><b>Now we will search $key in \$array</b><br/>"; $search=array_search($key,$array); echo"<br/><b>$key is found at $search</b><br/>";?>
Output:
Initially the values of $array is:
array(5) {
["new"]=>
int(11)
["delhi"]=>
int(12)
["common"]=>
int(13)
["wealth"]=>
int(14)
["game"]=>
int(15)
}
Now we will search 13 in $array
13 is found at common
Example 4:
<?php
$array=array("new"=>11,"delhi"=>12,"common","wealth"=>14,"game"=>15); echo"<br/><b>Initially the values of \$array is:</b><br/>";var_dump(
$array); $key="common"; echo"<br/><b>Now we will search \"$key\" in \$array</b><br/>"; $search=array_search($key,$array); echo"<br/><b>$key is found at $search</b><br/>";?>
Output:
Initially the values of $array is:
array(5) {
["new"]=>
int(11)
["delhi"]=>
int(12)
[0]=>
string(6) "common"
["wealth"]=>
int(14)
["game"]=>
int(15)
}
Now we will search "common" in $array
common is found at 0
Example 5:
<?php
$array=array("new"=>11,"delhi"=>12,13,"wealth"=>14,"game"=>15); echo"<br/><b>Initially the values of \$array is:</b><br/>";var_dump(
$array); $key=13; echo"<br/><b>Now we will search $key in \$array</b><br/>"; $search=array_search($key,$array); echo"<br/><b>$key is found at $search</b><br/>";?>
Output:
Initially the values of $array is:
array(5) {
["new"]=>
int(11)
["delhi"]=>
int(12)
[0]=>
int(13)
["wealth"]=>
int(14)
["game"]=>
int(15)
}
Now we will search 13 in $array
13 is found at 0
Example 6 (If we search a value which occurs more than one time):
<?php
$array=array("new","delhi","new","wealth","game"); echo"<br/><b>Initially the values of \$array is:</b><br/>";var_dump(
$array); $key="new"; echo"<br/><b>Now we will search \"$key\" in \$array</b><br/>"; $search=array_search($key,$array); echo"<br/><b>$key is found at $search</b><br/>";?>
Output:
Initially the values of $array is:
array(5) {
[0]=>
string(3) "new"
[1]=>
string(5) "delhi"
[2]=>
string(3) "new"
[3]=>
string(6) "wealth"
[4]=>
string(4) "game"
}
Now we will search "new" in $array
new is found at 0
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.