0
mixed array_search  ( mixed $needle  , array $haystack  [, bool $strict  ] )

If the third parameter strict  is set to TRUE  then the array_search() function will also check the types of the needle  in the haystack . 

I don't see what it means,maybe an example can help?

user198729
  • 61,774
  • 108
  • 250
  • 348

3 Answers3

3

If the last argument is true, it will use strict (also known as identity) comparison (===) when searching the array.

The equality comparison (==) compares the value where as the identity comparison (===) compares the value and the type.

'0' == 0 ; //true, the string is converted to an integer and then the are compared.
'0' === 0; //false, a string is not equal to a integer

You will find more information in this question How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?

This means that if you had an array of numbers

$a = array(0,1,2,3,4);

Using a strict comparison for the string value '2' will return false (not find a match) as there are no strings with the value '2'.

array_search('2', $a, true); //returns false

However if you don't do a strict search, the string is implicitly converted into an integer (or the other way around) and it returns the index of 2, as 2 == '2'

array_search('2', $a, false); //returns 2
Community
  • 1
  • 1
Yacoby
  • 54,544
  • 15
  • 116
  • 120
1

The third parameter tells the function to check also the types of needle and the haystack elements (i.e. to use a strict comparison ===).

<?php

$needle = "2"; //a string
$haystack = array (1,2,"2","test");

$search = array_search  ($needle  ,$haystack,  false);

// Will output 1, as it is the key for the second element of the array (an integer)
print_r($search);


$search = array_search  ($needle  ,$haystack,  true);

//Will output 2, as it is the key for the third element of the array (a string)
print_r($search);

?>
amercader
  • 4,490
  • 2
  • 24
  • 26
1

In array_search third argument is used for strict type checking.

For example if third argument is false , 123 is equal to "123" ==> true

if third argument is true , 123 is not equal to "123" ==> since both had different types .

Pavunkumar
  • 5,147
  • 14
  • 43
  • 69