php

How to check if a variable is an array in PHP?

In this tutorial, we are going to see how to check if a variable is an array in PHP?
 

is_array()

The function is_array($var) check if the passed variable is an array:

<?php
    var_dump( is_array(array()) ); // true
    var_dump( is_array(array(1, 2, 3)) ); // true
    var_dump( is_array($_SERVER) ); // true
 
    var_dump( is_array(1) ); // false
    var_dump( is_array(0) ); // false
    var_dump( is_array(2.555) ); // false
    var_dump( is_array('lorem') ); // false
    var_dump( is_array(true) ); // false
    var_dump( is_array(new stdClass()) ); // false
?>

Output:

bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
 
The use of is_array() to check function parameters is not absolutely necessary, since arrays can already be specified:

<?php
    function myFunction(array $a) {
        // ...
    }
 
    myFunction('hello'); // generates an error
?>

Output:

Fatal error:  Uncaught TypeError: Argument 1 passed to myFunction() must be of the type array, string given, called in [...][...] on line 6 and defined in [...][...]:2
Stack trace:
#0 [...][...](6): myFunction('hello')
#1 {main}
  thrown in [...][...] on line 2
PHP MCQ - Multiple Choice Questions and AnswersPHP Questions and Answers – Object-Oriented OOP – Part 1This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Object-Oriented OOP”.   1. The concept of…Read More
Function parameters:

The function is_array() can be useful when a function parameter should usually be an array, but can optionally have other data types. The following example defines a function “searchMultipleUser”, which is called with multiple names or ID numbers – passed as array. But there is also the option to pass only a string (a single user name) or an integer (a single ID). The string or ID is then automatically converted into an array.

<?php
    function searchMultipleUser($name) {
        if (!is_array($name)) {
            // A name was given?
            if (!is_string($name) && !is_int($name)) {
                throw new Exception('String/integer or array of strings/integers expected.');
            } else {
                return searchMultipleUser(array($name));
            }
        } else {
            // search
            // here, we output the names / IDs
            echo("search for: ");
            array_walk($name, function($n) { echo "$n, "; });
            echo("\n");
        }
    }
 
    searchMultipleUser('Alex');
    searchMultipleUser(10001);
    searchMultipleUser(array('Alex', 'Bob', 'Ali'));
?>

Output:

search for: Alex, 
search for: 10001, 
search for: Alex, Bob, Ali,
 
In the next example, a parameter is completely optional, but must be an array if defined by the developer. If it is not passed, it should be automatically filled with an empty array.

<?php
    function check(array $data=null) {
        // check if $data is an array
        if (!is_array($data)) {
            // here some default values could be set
            $data = array();
        }
 
        var_dump($data);
    }
 
    check(array(1, 2, 3, 4));
    check();
?>

Output:

array(4) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
}
array(0) {
}
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *