is_array() tests whether a value is an array and returns true or false. It is useful before applying operations that specifically require array input.
$data = ['id' => 10];
if (is_array($data)) {
echo 'Array received';
}Do not use is_array() when you only need to know whether something is iterable; arrays and Traversable objects are different PHP types.
$a= array("Three", "two", "Four", "five","ten");
$b="Hello Welcome to plus2net ";
if(is_array($a)){ // Change this variable to $b
echo " This is an array ";
}else{
echo " This is not an array ";
}
The above code will return This is an array if $a is checked using if condition.
<?Php
$str="Learn web programming at plus2net.com";
echo is_array($str) ? 'Yes, Array' : 'not an Array';
?>
Output is
not an Array
By using split function we can break a sting and create an array. Here is the modified code.
<?Php
$str=split(" ","Learn web programming at plus2net.com");
echo is_array($str) ? 'Yes, Array' : 'not an Array';
?>
Output is
Yes, Array
<?php
$array = ['name' => 'John', 'age' => 25];
echo is_array($array) ? 'Yes, Array' : 'Not an Array'; // Output: Yes, Array
?>
<?php
$nestedArray = [1, [2, 3], 4];
echo is_array($nestedArray[1]) ? 'Nested is Array' : 'Nested is not an Array'; // Output: Nested is Array
?>
<?php
$var = 'Hello';
echo is_array($var) ? 'Array' : 'Not an Array'; // Output: Not an Array
?>
<?php
$input = [10, 20, 30];
if (is_array($input)) {
echo "The input is an array!";
} else {
echo "The input is not an array!";
}
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.