Check array value in php
PHP having some in-built function to check the value is exists or not in array.- in_array()
- array_search()
in_array()
This function will check the given value with array and if it the value is exists in the array, return true otherwise return false.
in_array(your_value,array,strict);
i) Simple Array
The following example will tell you, how to check the value is exists or not in simple array.
$place=array("mulagumoodu","India","Tamilnadu","USA");
if(in_array("USA",$place))
{
echo "USA Found in $place Array";
}
else
{
echo "USA Not Found in $place Array";
}
Output:
USA Found in $place Array
ii) Associative Array
Associative array also similarly works simple array.
$place=array(0=>"mulagumoodu",1=>"India",2=>"Tamilnadu",3=>"USA");
if(in_array("USA",$place))
{
echo "USA Found in $place Array";
}
else
{
echo "USA Not Found in $place Array";
}
Output:
USA Found in $place Array
array_search()
This function is used to find the given value is exists or not in an array and if exists return the key value otherwise return NULL.
array_search(your_value,array,strict);
Example
$place=array(0=>"mulagumoodu",1=>"India",2=>"Tamilnadu",3=>"USA");
echo array_search("USA",$place);
Output:
3
0 comments:
Post a Comment