35

Trying to figure out how to do the equivalent of something I did in javascript but in php. But I'm not sure of the operators to do it. In javascript I wanted to see if a particular parameter being passed was either an object or array.. and if not then was it a string/int and what I did was something like

if (str instanceof Array || str instanceof Object) 
{
   //code
}
else
{
   //code
}

anyone know of the equivalent to this for php?

6 Answers 6

82

Use is_array to check if a variable is an array, and similarly, use is_object to check if a variable is an object.

Sign up to request clarification or add additional context in comments.

3 Comments

yea, its funny.. no sooner than I typed it out.. i remembered is_array then I looked up to see if theres similar for objects.. im super clever like that today apparently, thanks. By the way is there by chance anything like this for JSON cause I'd like to throw that in my mix so if it is JSON i can decode/encode accordingly to work with it
@chris: I suppose you can try to json_decode it, and if it fails, it's not JSON.
You should never optimize prematurely, but if is_array($var) turns out to be your bottleneck after profiling then (array) $var === $var is more performant
12

Try to use:

if (!is_scalar($var)) {
    // Varible is object or array
}

3 Comments

What about "resource" types?
As mentioned @OMA this matches NULL and resources as well.
PHP.net says: Note: is_scalar() does not consider resource type values to be scalar as resources are abstract datatypes which are currently based on integers. This implementation detail should not be relied upon, as it may change.
3

PHP >= 8.1

From PHP 8.1 and above, you can use:

array_is_list($array)

https://www.php.net/manual/en/function.array-is-list.php


PHP < 8.1

Pure is_array/is_object is unable to judge because:

is_array([0,1,2,3]) // true
is_object([0,1,2,3]) // false

is_array(['a'=>1,'b'=>2]) // true
is_object(['a'=>1,'b'=>2]) // false

Here i wrote a simple function to do it

function isRealObject($arrOrObject) {
    if (is_object($arrOrObject))
        return true;
    $keys = array_keys($arrOrObject);
    return implode('', $keys) != implode(range(0, count($keys)-1));
}

4 Comments

yeah...just noticed this new method , we are still using 7.x
This answer is technically incorrect because an array is still an array in PHP even if it has gaps in its numeric keys or is associative. Why is this answer upvoted? implode() glue can be omitted when an empty string is the desired glue. It seems that you've posted on the wrong page. stackoverflow.com/q/173400/2943403 already covers the topic that you are trying to cover.
@mickmackusa yes it is. but it should be a historical fault in php, in most languages , list array and object is obviously different. if you call json_object/json_array in mysql8 , you will know why we care for this
1

I came across this question while looking for is_countable. Maybe it's of some use to somebody. https://www.php.net/manual/en/function.is-countable.php

Comments

0

object (use is_object)-----

stdClass Object
(
    [rest_food_items_id] => 137
    [rest_user_id] => 42
)

array (use is_array)----

Array
(
    [rest_food_items_id] => 137
    [rest_user_id] => 42
)

**

Example

**

if(is_object($data)){

}
if(is_array($data)){

}

2 Comments

it's right, for check array in object, and normal in array.
The accepted answer already provided these insights 5 years earlier on this page.
0

As of PHP 8.0, you can use Union types when writing functions, validating your paramter's type at runtime:

function test(array|object $something): void
{
    // Here, $something is either an array or an object
}

1 Comment

This answer is ignoring that the asked question has an else branch to handle non-array/non-object data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.