The PHP in_array function USES the instructions with the in_array note

  • 2020-03-31 20:37:36
  • OfStack

in_array
(PHP 4, PHP 5)

In_array - checks for the presence of a value in the array

instructions
bool in_array ( mixed $needle , array $haystack [, bool $strict ] )

Search haystack for needle and return TRUE if found, or FALSE otherwise.

The in_array() function also checks that the needle is of the same type as haystack if the strict value of the third parameter is TRUE.

Note: if a needle is a string, the comparison is case sensitive.

Note: before PHP version 4.2.0, needle was not allowed to be an array.

Example #1: in_array()
 
<?php 
$os = array("Mac", "NT", "Irix", "Linux"); 
if (in_array("Irix", $os)) { 
echo "Got Irix"; 
} 
if (in_array("mac", $os)) { 
echo "Got mac"; 
} 
?> 

The second condition failed because in_array() is case-sensitive, so the above program is shown as:
Got Irix

Example #2. Example of strict type checking in_array()
 
<?php 
$a = array('1.10', 12.4, 1.13); 

if (in_array('12.4', $a, true)) { 
echo "'12.4' found with strict checkn"; 
} 
if (in_array(1.13, $a, true)) { 
echo "1.13 found with strict checkn"; 
} 
?> 

The above example will output:

1.13 found with strict check

Example #3 in_array() USES an array as a needle
 
<?php 
$a = array(array('p', 'h'), array('p', 'r'), 'o'); 

if (in_array(array('p', 'h'), $a)) { 
echo "'ph' was foundn"; 
} 
if (in_array(array('f', 'i'), $a)) { 
echo "'fi' was foundn"; 
} 
if (in_array('o', $a)) { 
echo "'o' was foundn"; 
} 
?> 

The above example will output:

'ph' was found
'o' was found

Points to note:

If:

Declare an array as:

$arr = array (*);

Then there are:

  In_array (0, $arr) = = true

 

Inexplicable! {weak language}


Solutions:
      In_array (strval (0), $arr, true))

Related articles: