To check if a specified element is present in an array in PHP, you can use functions like in_array
.
In this example,
$numbers
with values 10, 20, 30, 40, 50.in_array
function to check if element 30 is present in the array.in_array
function returns true if the element is found, otherwise false.<?php
$numbers = [10, 20, 30, 40, 50];
$isPresent = in_array(30, $numbers);
echo 'Element 30 is present: ' . ($isPresent ? 'true' : 'false');
?>
Element 30 is present: true
In this example,
$fruits
with values 'apple', 'banana', 'cherry'.in_array
function to check if element 'banana' is present in the array.in_array
function returns true if the element is found, otherwise false.<?php
$fruits = ['apple', 'banana', 'cherry'];
$isPresent = in_array('banana', $fruits);
echo 'Element "banana" is present: ' . ($isPresent ? 'true' : 'false');
?>
Element "banana" is present: true
In this tutorial, we learned How to check if Specified Element is present in the Array in PHP language with well detailed examples.