in_array function in PHP, How to find an element in an array in PHP, Difference between in_array() and array_search() function in PHP
If you want to determine whether a value exists within a provided array, use the in_array() function. This function checks data within the array in question and returns true or false accordingly. If you want to get the value of the searched element rather then the true or false value use the array_search() function.
Difference between in_array() and array_search() function in PHP
in_array() used to check if the element is exist or not into the given array and the array_search() used to print the element which we want to find.
Example
<?php
$marks = array(87,88,98,74,56,94,67,98,49) ;
echo "<b>The all elements of the Array are</b><br/>";
foreach($marks as $mark)
echo "$mark<br/>" ;
if (in_array(49, $marks) ) {
echo "The 49 Element is found at the position : " . array_search(49, $marks) ;
} else {
echo "49 Not found in given array" ;
}
?>
Output
The all elements of the Array are
87
88
98
74
56
94
67
98
49The 49 Element is found at the position 8
If you want to determine whether a value exists within a provided array, use the in_array() function. This function checks data within the array in question and returns true or false accordingly. If you want to get the value of the searched element rather then the true or false value use the array_search() function.
Difference between in_array() and array_search() function in PHP
in_array() used to check if the element is exist or not into the given array and the array_search() used to print the element which we want to find.
Example
<?php
$marks = array(87,88,98,74,56,94,67,98,49) ;
echo "<b>The all elements of the Array are</b><br/>";
foreach($marks as $mark)
echo "$mark<br/>" ;
if (in_array(49, $marks) ) {
echo "The 49 Element is found at the position : " . array_search(49, $marks) ;
} else {
echo "49 Not found in given array" ;
}
?>
Output
The all elements of the Array are
87
88
98
74
56
94
67
98
49The 49 Element is found at the position 8
{ 0 comments... read them below or add one }
Post a Comment