Reputation: 9076
I am using the following code to search an element inside a multi dimensional array.When a match is found it returns the index.But when no match is found it returns a empty value.Not 0 or 1.But if i print out the type , it says boolean.What does that boolean mean if it returns empty.Does it mean that it will be equal to empty string ?
$arr =Array
(
0 => Array
(
'uid' => '100',
'name' => 'Sandra Shush'
),
1 => Array
(
'uid' => '5465',
'name' => 'Stefanie Mcmohn'
),
2 => Array
(
'uid' => '40489',
'name' => 'Michael'
)
);
$match = array_search('546',array_column($arr, 'uid'));
echo gettype($match);
Upvotes: 0
Views: 321
Reputation: 72286
array_search()
returns FALSE
when it cannot find the needle in the haystack.
The comparison is done using ==
. This means, the values are converted to the same type, if needed. But their values must be the same after conversion. It doesn't match substrings.
echo(FALSE);
doesn't print anything. The type of FALSE
is boolean
, indeed. And FALSE
is ==
with the empty string (''
), zero (0
), the string that contain the number zero ('0'
) and other empty values.
Upvotes: 1
Reputation: 5690
your code is working , when array_search not found then return false
and if gettype($match); then show boolean and if found then return index so in this case return integer
this is code which return
<?php
$arr =Array
(
0 => Array
(
'uid' => '100',
'name' => 'Sandra Shush'
),
1 => Array
(
'uid' => '5465',
'name' => 'Stefanie Mcmohn'
),
2 => Array
(
'uid' => '40489',
'name' => 'Michael'
)
);
$match = array_search('5465',array_column($arr, 'uid'));
echo gettype($match);
and output is: integer
this is normal example
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
?>
for more information
http://php.net/manual/ro/function.array-search.php
Upvotes: 2
Reputation: 26278
array_search()
function returns the key for needle if it is found in the array, FALSE otherwise. So when you are using gettype()
on the return value, then it will return the type of FALSE
i.e. BOOLEAN
for unsuccessful search, otherwise INT
index value.
Upvotes: 2
Reputation: 1108
http://php.net/manual/en/function.array-search.php
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource and NAN).
Upvotes: 2
Reputation: 200
If you take a look at array_search method description http://php.net/manual/ro/function.array-search.php it says: Returns the key for needle if it is found in the array, FALSE otherwise. There is your answer.
Upvotes: 2