PHP Array Sub String Search


 

PHP Array Sub String Search

PHP Array Sub String Search: In this tutorial you will learn how to search a sub string in an array. PHP provides in_array() function to search a sub string.

PHP Array Sub String Search: In this tutorial you will learn how to search a sub string in an array. PHP provides in_array() function to search a sub string.

PHP Array Search Sub-String

In PHP if we need to search a sub-string within an array then we can use in_array() function. The format of in_array is much similar to array_search() function.

General Desciption of in_array() function is as follows:

General Format Boolean in_array ( 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

PHP Search Array Substring Example 1:

<?php

$array1=array("India","Land","of","diversity");

echo"<br/><b>The value of \$array1 is:</b><br/>";

var_dump($array1);

$search="India";

echo"<br/><b>The value to be search is \"$search\"</b><br/>";

if(in_array($search,$array1,true))

    echo "The string: \"".$search."\" Found";

else

    echo"Not found";

?>

Output:

The value of $array1 is:
array(4) {
  [0]=>
  string(5) "India"
  [1]=>
  string(4) "Land"
  [2]=>
  string(2) "of"
  [3]=>
  string(9) "diversity"
}

The value to be search is "India"
The string: "India" Found

 

Ads