33

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?

chris
  • 36,115
  • 52
  • 143
  • 252

6 Answers6

79

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

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 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 Jun 08 '12 at 23:27
  • 3
    @chris: I suppose you can try to [`json_decode`](http://php.net/json_decode) it, and if it fails, it's not JSON. – Ry- Jun 08 '12 at 23:27
  • 1
    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 – rgvcorley Apr 28 '15 at 14:37
12

Try to use:

if (!is_scalar($var)) {
    // Varible is object or array
}
WOLFF
  • 586
  • 4
  • 7
  • 1
    What about "resource" types? – OMA Oct 06 '15 at 18:05
  • 2
    As mentioned @OMA this matches NULL and resources as well. – Yarik Dot Apr 26 '16 at 11:04
  • 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. – Theodore R. Smith Jul 29 '19 at 00:21
1

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)){

}
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

Yani
  • 424
  • 6
  • 13
1

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
}
MAChitgarha
  • 3,728
  • 2
  • 33
  • 40
1

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));
}
Typewar
  • 825
  • 1
  • 13
  • 28
Del Xiong
  • 19
  • 2
  • 1
    You're a bit late, https://www.php.net/manual/en/function.array-is-list.php – Your Common Sense Jul 12 '22 at 09:20
  • yeah...just noticed this new method , we are still using 7.x – Del Xiong Jul 13 '22 at 10:10
  • 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. https://stackoverflow.com/q/173400/2943403 already covers the topic that you are trying to cover. – mickmackusa Aug 22 '22 at 03:53
  • @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 – Del Xiong Aug 23 '22 at 04:22