array_searchSEARCHES the ARRAY for a given value and returns the first corresponding key.
This function returns the key for $needle if it is found in the ARRAY, FALSE otherwise..
If $needle is a STRING, the comparison is done in a case-sensitive manner.
If $needle is found in $haystack more than once, the first matching key is returned.
<?php
mix array_search ( mix $needle , arr $haystack [, bool $strict = FALSE ] )
where,
%needle = The value to be searched
$array = The working array
$strict = To control whether identical elements or not
?>
$needle
The searched value.
$haystack
The given ARRAY.
$strict
To control if the search is made for identical elements or not.
STRICT MODE |
VALUES |
PURPOSE |
0 (FALSE) |
=== will not be used |
1 (TRUE) |
=== will be used |
ed48 |
EXERCISE
<?php
$arr01a = array(2, 8, 18, 32, array (32, 18, 8));
$arr01b = [ 2, 8, 18, 32, [ 32, 18, 8 ] ];
$arr01c = [ 2, 8, '18', 32, [ 'THIRTY-TWO', "eighteen", 8 ] ];
$arr01d= array("K" => 2, "L" => 8,
"M" => 18, "N" => 32,
"O" => 32, "P" => 18, "Q" => 8);
$arr01e = [ 299792458, 6.67428E-11, [ 'c', "G" ] ];
$arr01f = ['01023', '0B0011', '0x20b',
['octal', 'binary', 'hexadecimal' ] ];
$arr01g = ["Countries", "continentes",
["Brasil", "Portugal", "Japan"],
["South America", "Europe", "Asia"] ];
$arr01h = [ "RGB" => ["RED", "GREEN", "BLUE"],
"HSB" => ["HUE", "SATURATION", "BLACK"] ];
$arr01i = ["HUE", "SATURATION", "BLACK" ];
$valarr01 = [ 0, 2, 8, 18, '18', 32,
'THIRTY-TWO', "eighteen",
299792458, '0x20b', "continentes",
'rgb', 'RGB', 'black', 'BLACK',
'hue', 'HUE' ];
$vala01 = mt_rand(0, count($valarr01, 1) - 1);
$val = $valarr01[$vala01];
echo '<br>Element to be searched: ' . $val .
' (' . gettype($val) . ')<br><br>';
function arrsearch01($val, $arr)
{
$ser01 = array_search($val, $arr);
if($ser01)
{
echo 'The ARRAY given:<br>';
print_r($arr);
echo '<br>The index of the searched element is ';
var_export ($ser01);
echo '<br><br>';
}
else
{
echo 'The ARRAY given:<br>';
print_r($arr);
echo '<br>THE ELEMENT SEARCHED, WAS NOT FOUND!<br><br>';
}
}
arrsearch01($val, $arr01a);
arrsearch01($val, $arr01b);
arrsearch01($val, $arr01c);
arrsearch01($val, $arr01d);
arrsearch01($val, $arr01e);
arrsearch01($val, $arr01f);
arrsearch01($val, $arr01g);
arrsearch01($val, $arr01h);
arrsearch01($val, $arr01i);
?>
EXERCISE
<?php
$arr02 = [ "RGB" => ["RED", "GREEN", "BLUE"],
"HSB" => ["HUE", "SATURATION", "BLACK"] ];
$val02 = ["HUE", "SATURATION", "BLACK"];
$ser02 = array_search($val02, $arr02);
if($ser02)
{
echo 'The ARRAY given:<br>';
print_r($arr02);
echo '<br>The index of the searched element is ';
print_r($ser02);
echo '.':
}
else
{
echo 'The ARRAY given:<br>';
print_r($arr02);
echo '<br>THE ELEMENT SEARCHED, WAS NOT FOUND!<br><br>';
}
?>
RESULT
The ARRAY given:
Array ( [RGB] => Array ( [0] => RED [1] => GREEN [2] => BLUE ) [HSB] => Array ( [0] => HUE [1] => SATURATION [2] => BLACK ) )
The index of the searched element is HSB.
EXERCISE
<?php
$arr03 = [ "RGB" => ["RED", "GREEN", "BLUE"],
"HSB" => ["HUE", "SATURATION", "BLACK"] ];
$val03 = ["hue", "saturation", "black"];
$ser03 = array_search($val03, $arr03);
if($ser03)
{
echo 'The ARRAY given:<br>';
print_r($arr03);
echo '<br>The index of the searched element is ';
print_r($ser03);
}
else
{
echo 'The ARRAY given:<br>';
print_r($arr03);
echo '<br>THE ELEMENT SEARCHED, WAS NOT FOUND!<br><br>';
}
?>
RESULT
The ARRAY given:
Array ( [RGB] => Array ( [0] => RED [1] => GREEN [2] => BLUE ) [HSB] => Array ( [0] => HUE [1] => SATURATION [2] => BLACK ) )
THE ELEMENT SEARCHED, WAS NOT FOUND!
EXERCISE
<?php
$var04 = array("one" => 1, "two" => 2, "THREE" => "3");
$val04 = 3;
$ser04 = array_search($val04, $var04);
if($ser04)
{
echo 'The ARRAY given:<br>';
print_r($var04);
echo '<br>The index of the searched element is ';
print_r($ser04);
echo '.';
}
else
{
echo 'The ARRAY given:<br>';
print_r($var04);
echo '<br>THE ELEMENT SEARCHED, WAS NOT FOUND!<br><br>';
}
?>
RESULT
The ARRAY given:
Array ( [one] => 1 [two] => 2 [THREE] => 3 )
The index of the searched element is THREE.