Php проверить что это массив
Перейти к содержимому

Php проверить что это массив

  • автор:

PHP — is_array() Function

The function is_array() is used to check whether a variable is an array or not.

Syntax

Parameters

The variable being evaluated

Return Values

This function returns boolean value true if value is an array, otherwise returns false.

Dependencies

PHP 4 and above

Example

Following example demonstrates use of is_array() function −

Output

This will produce following result −

Annual Membership

Enjoy unlimited access on 5500+ Hand Picked Quality Video Courses

Training for a Team

Affordable solution to train a team and make them project ready.

Copyright © TUTORIALS POINT (INDIA) PRIVATE LIMITED. All Rights Reserved.

We make use of First and third party cookies to improve our user experience. By using this website, you agree with our Cookies Policy. Agree Learn more

is_array

Возвращает true , если переменная value является массивом ( array ), или false в противном случае.

Примеры

Пример #1 Проверяет, является ли переменная массивом

<?php
$yes = array( ‘это’ , ‘массив’ );

echo is_array ( $yes ) ? ‘Массив’ : ‘Не массив’ ;
echo «\n» ;

echo is_array ( $no ) ? ‘Массив’ : ‘Не массив’ ;
?>

Результат выполнения данного примера:

Смотрите также

  • array_is_list() — Проверяет, является ли данный array списком
  • is_float() — Проверяет, является ли переменная числом с плавающей точкой
  • is_int() — Проверяет, является ли переменная целым числом
  • is_string() — Проверяет, является ли переменная строкой
  • is_object() — Проверяет, является ли переменная объектом

User Contributed Notes 21 notes

Please note that the ‘cast to array’ check is horrendously out of date.

Running that code against PHP 5.6 results in this:

is_array : 0.93975400924683
cast, === : 1.2425191402435

So, please use ‘is_array’, not the horrible casting hack.

hperrin’s results have indeed changed in PHP 7. The opposite is now true, is_array is faster than comparison:

is_array : 0.52148389816284
cast, === : 0.84179711341858

Tested 1000000 iterations.

Or you could make use of the array_diff_key and array_key function:

function is_assoc ( $var )
<
return is_array ( $var ) && array_diff_key ( $var , array_keys ( array_keys ( $var )));
>

function test ( $var )
<
echo is_assoc ( $var ) ? «I’m an assoc array.\n» : «I’m not an assoc array.\n» ;
>

// an assoc array
$a = array( «a» => «aaa» , «b» => 1 , «c» => true );
test ( $a );

// an array
$b = array_values ( $a );
test ( $b );

// an object
$c = (object) $a ;
test ( $c );

// other types
test ( $a -> a );
test ( $a -> b );
test ( $a -> c );

?>

The above code outputs:
I’m an assoc array.
I’m not an assoc array.
I’m not an assoc array.
I’m not an assoc array.
I’m not an assoc array.
I’m not an assoc array.

Yet another simpler, faster is_assoc():

<?php
function is_assoc ( $array ) <
foreach ( array_keys ( $array ) as $k => $v ) <
if ( $k !== $v )
return true ;
>
return false ;
>
?>

In my tests it runs about twice as fast as Michael/Gabriel’s array_reduce() method.

(Speaking of which: Gabriel’s version doesn’t work as written; it reports associative arrays as numeric if only the first key is non-numeric, or if the keys are numeric but ordered backwards. Michael solves this problem by comparing array_reduce() to count(), but that costs another function call; it also works to just compare to -1 instead of 0, and therefore return -1 as the ternary else from the callback).

I’ve found a faster way of determining an array. If you use is_array() millions of times, you will notice a *huge* difference. On my machine, this method takes about 1/4 the time of using is_array().

Cast the value to an array, then check (using ===) if it is identical to the original.

<?php
if ( (array) $unknown !== $unknown ) <
echo ‘$unknown is not an array’ ;
> else <
echo ‘$unknown is an array’ ;
>
?>

You can use this script to test the speed of both methods.

<pre>
What’s faster for determining arrays?

$test = array( ‘im’ , ‘an’ , ‘array’ );
$test2 = ‘im not an array’ ;
$test3 = (object) array( ‘im’ => ‘not’ , ‘going’ => ‘to be’ , ‘an’ => ‘array’ );
$test4 = 42 ;
// Set this now so the first for loop doesn’t do the extra work.
$i = $start_time = $end_time = 0 ;

$start_time = microtime ( true );
for ( $i = 0 ; $i < $count ; $i ++) <
if (! is_array ( $test ) || is_array ( $test2 ) || is_array ( $test3 ) || is_array ( $test4 )) <
echo ‘error’ ;
break;
>
>
$end_time = microtime ( true );
echo ‘is_array : ‘ .( $end_time — $start_time ). «\n» ;

$start_time = microtime ( true );
for ( $i = 0 ; $i < $count ; $i ++) <
if (!(array) $test === $test || (array) $test2 === $test2 || (array) $test3 === $test3 || (array) $test4 === $test4 ) <
echo ‘error’ ;
break;
>
>
$end_time = microtime ( true );
echo ‘cast, === : ‘ .( $end_time — $start_time ). «\n» ;

echo «\nTested $count iterations.»

Prints something like:

What’s faster for determining arrays?

is_array : 7.9920151233673
cast, === : 1.8978719711304

Tested 1000000 iterations.

alex frase’s example is fast but elanthis at awesomeplay dot com’s example is faster and Ilgar’s modification of alex’s code is faulty (the part » || $_array[$k] !== $v»). Also, Ilgar’s suggestion of giving a false return value when the variable isnt an array is not suitable in my opinion and i think checking if the array is empty would also be a suitable check before the rest of the code runs.

So here’s the modified (is_vector) version

<?php
function is_vector ( & $array ) <
if ( ! is_array ( $array ) || empty( $array ) ) <
return — 1 ;
>
$next = 0 ;
foreach ( $array as $k => $v ) <
if ( $k !== $next ) return true ;
$next ++;
>
return false ;
>
?>

and the modified (alex’s is_assoc) version

<?php
function is_assoc ( $_array ) <
if ( ! is_array ( $_array ) || empty( $array ) ) <
return — 1 ;
>
foreach ( array_keys ( $_array ) as $k => $v ) <
if ( $k !== $v ) <
return true ;
>
>
return false ;
>
?>

function is_associate_array($array)
<
return $array === array_values($array);
>

or you can add check is_array in functions

The is_associative_array() and is_sequential_array() functions posted by ‘rjg4013 at rit dot edu’ are not accurate.

The functions fail to recognize indexes that are not in sequence or in order. For example, array(0=>’a’, 2=>’b’, 1=>’c’) and array(0=>’a’, 3=>’b’, 5=>’c’) would be considered as sequential arrays. A true sequential array would be in consecutive order with no gaps in the indices.

The following solution utilizes the array_merge properties. If only one array is given and the array is numerically indexed, the keys get re-indexed in a continuous way. The result must match the array passed to it in order to truly be a numerically indexed (sequential) array. Otherwise it can be assumed to be an associative array (something unobtainable in languages such as C).

The following functions will work for PHP >= 4.

<?php
function is_sequential_array ( $var )
<
return ( array_merge ( $var ) === $var && is_numeric ( implode ( array_keys ( $var ) ) ) );
>

function is_assoc_array ( $var )
<
return ( array_merge ( $var ) !== $var || ! is_numeric ( implode ( array_keys ( $var ) ) ) );
>
?>

If you are not concerned about the actual order of the indices, you can change the comparison to == and != respectively.

I would change the order of the comparison, because if it is really an empty array, it is better to stop at that point before doing several ‘cpu & memory intensive’ function calls.

In the end on a ratio of 3 not empty arrays to 1 empty array computed for 1000000 iterations it needed 10% less time.
Or the other way round:
It needed approx 3% to 4% more time if the array is not empty, but was at least 4 times faster on empty arrays.

Additionally the memory consumption veritably lesser.

<?php
function is_assoc ( $array ) <
return ( is_array ( $array ) && ( count ( $array )== 0 || 0 !== count ( array_diff_key ( $array , array_keys ( array_keys ( $array ))) )));
>
?>

Using is_array prior to an in_array within an if clause will safely escape a check against a variable that could potentially be a non-array when using in_array. For instance:

NOTE: A real use case might be that we have a list of possible flags which in a database we have stored whether each of the flags are 0 or 1. We want a list of the flags which have the value of 1 to be returned.

Our example here will not use so many technical artifacts, but will be based on similar logic just to get the point across.

// We have a list of known values
$knownVars = [ ‘apple’ , ‘orange’ ];

// A list of values to check
$listToCheck = [ ‘pear’ , ‘banana’ ];

// And a method that takes a list of values to check and returns a new list
// of the items from said list that are found to be valid.
public function getValidItemsList ( $listToCheck /*[‘pear’, ‘banana’]*/ )
<
$returnList = [];
foreach( $listToCheck as $key => $val )
<
if( in_array ( $val , $knownVars ))
<
array_push ( $returnList , $val );
>
>

if(empty( $returnList ))
<
// We have a special case if there were no valid items found, which is the case we are going over
return — 1 ;
>

// Otherwise, normally returns a list of the items that were found to be valid
return $returnList ;
>

// Call the method and check for any valid items that can be used for some purpose
$validItemsList = getValidItemsList ( $listToCheck );

// In this usage we could potentially get an exception because
// in_array() expects an array for argument #2, checking that the value != -1 does not escape the if statement:
if(isset( $validItemsList ) && $validItemsList != — 1 && in_array ( ‘apple’ , $validItemsList ))
<
//.
>

// In this usage, we break free from the if statement safely:
if(isset( $validItemsList ) && $validItemsList != — 1 && is_array ( $validItemsList ) && in_array ( ‘apple’ , $validItemsList ))
<
//.
>

?>

Hope that can help someone, I know it helped me.

A slight modification of what’s below:

function is_assoc ( $array )
<
return is_array ( $array ) && count ( $array ) !== array_reduce ( array_keys ( $array ), ‘is_assoc_callback’ , 0 );
>

function is_assoc_callback ( $a , $b )
<
return $a === $b ? $a + 1 : 0 ;
>

And here is another variation for a function to test if an array is associative. Based on the idea by mot4h.

<?php
function is_associative ( $array )
<
if (! is_array ( $array ) || empty( $array ))
return false ;

$keys = array_keys ( $array );
return array_keys ( $keys ) !== $keys ;
>
?>

function is_assoc1 ( $array ) <
if (! is_array ( $array )) return false ;
$i = count ( $array );
while ( $i > 0 ) unset( $array [— $i ]);
return (bool) $array ;
>

function is_assoc2 (& $array ) <
if (! is_array ( $array )) return false ;
$i = count ( $array );
while ( $i > 0 ) <
if (!isset( $array [— $i ])) return true ;
>
return false ;
>

function is_assoc3 (& $array ) <
if (! is_array ( $array )) return false ;
$i = count ( $array );
while ( $i > 0 ) <
if (! array_key_exists (— $i , $array )) return true ;
>
return false ;
>

function is_assoc4 ( $array ) <
if (! is_array ( $array )) return false ;
ksort ( $array );
foreach ( array_keys ( $array ) as $k => $v ) <
if ( $k !== $v ) return true ;
>
return false ;
>

function is_assoc5 (& $array ) <
return is_array ( $array ) && array_diff_key ( $array , array_keys ( $array ));
>

$arr1 = array(); // not associative
$arr2 = $arr3 = array( ‘foo’ , ‘bar’ , ‘baz’ , ‘foo’ , ‘bar’ , ‘baz’ , ‘foo’ , ‘bar’ , ‘baz’ , ‘foo’ ); // not associative
asort ( $arr3 ); // not associative, shuffled keys
$arr4 = array( ‘foo’ , ‘bar’ , ‘baz’ , ‘foo’ , ‘bar’ , null , ‘foo’ , ‘bar’ , ‘baz’ , ‘foo’ ); // not associative but is_assoc2() thinks it is
$arr5 = array( 0 => ‘foo’ , 1 => ‘bar’ , 2 => ‘baz’ , 3 => ‘foo’ , 4 => ‘bar’ , 5 => ‘baz’ , ‘foo3’ => ‘foo’ , ‘bar3’ => ‘bar’ , ‘baz3’ => ‘baz’ , ‘foo4’ => ‘foo’ ); // associative

$i = $j = 0 ;
$time = array( 0.0 , 0.0 , 0.0 , 0.0 , 0.0 );

for ( $j = 0 ; $j < 2000 ; $j ++) <
$time [ 0 ] -= microtime ( true );
for ( $i = 0 ; $i < 1000 ; $i ++) <
if ( is_assoc1 ( $arr1 ) || is_assoc1 ( $arr2 ) || is_assoc1 ( $arr3 ) || is_assoc1 ( $arr4 ) || ! is_assoc1 ( $arr5 )) <
echo ‘error’ ;
break;
>
>
$time [ 0 ] += microtime ( true );
$time [ 1 ] -= microtime ( true );
for ( $i = 0 ; $i < 1000 ; $i ++) <
if ( is_assoc2 ( $arr1 ) || is_assoc2 ( $arr2 ) || is_assoc2 ( $arr3 ) || ! is_assoc2 ( $arr4 ) || ! is_assoc2 ( $arr5 )) < // $arr4 tweaked
echo ‘error’ ;
break;
>
>
$time [ 1 ] += microtime ( true );
$time [ 2 ] -= microtime ( true );
for ( $i = 0 ; $i < 1000 ; $i ++) <
if ( is_assoc3 ( $arr1 ) || is_assoc3 ( $arr2 ) || is_assoc3 ( $arr3 ) || is_assoc3 ( $arr4 ) || ! is_assoc3 ( $arr5 )) <
echo ‘error’ ;
break;
>
>
$time [ 2 ] += microtime ( true );
$time [ 3 ] -= microtime ( true );
for ( $i = 0 ; $i < 1000 ; $i ++) <
if ( is_assoc4 ( $arr1 ) || is_assoc4 ( $arr2 ) || is_assoc4 ( $arr3 ) || is_assoc4 ( $arr4 ) || ! is_assoc4 ( $arr5 )) <
echo ‘error’ ;
break;
>
>
$time [ 3 ] += microtime ( true );
$time [ 4 ] -= microtime ( true );
for ( $i = 0 ; $i < 1000 ; $i ++) <
if ( is_assoc5 ( $arr1 ) || is_assoc5 ( $arr2 ) || is_assoc5 ( $arr3 ) || is_assoc5 ( $arr4 ) || ! is_assoc5 ( $arr5 )) <
echo ‘error’ ;
break;
>
>
$time [ 4 ] += microtime ( true );
>

echo ‘is_assoc1(): ‘ . $time [ 0 ] . «\n» ;
echo ‘is_assoc2(): ‘ . $time [ 1 ] . «\n» ;
echo ‘is_assoc3(): ‘ . $time [ 2 ] . «\n» ;
echo ‘is_assoc4(): ‘ . $time [ 3 ] . «\n» ;
echo ‘is_assoc5(): ‘ . $time [ 4 ] . «\n» ;

?>

is_assoc1() — uses unset(), a bit slow, but mem friendly and no function calls
is_assoc2() — uses isset(), fastest one, but returns TRUE whenever array contains NULL
is_assoc3() — fixed is_assoc2(), uses array_key_exists(), fast and memory friendly, and much smarter than the following (no need to check all those keys)
is_assoc4() — alex’ version with proper check and key sorting
is_assoc5() — fixed a bit JTS’ version, really nice one, but uses too many functions and checks all keys

is_assoc1(): 2.1628699302673
is_assoc2(): 1.1079933643341
is_assoc3(): 1.7120850086212
is_assoc4(): 3.9194552898407
is_assoc5(): 1.9509885311127

PHP: is_array() function

The is_array() function is used to find whether a variable is an array or not.

Version:

(PHP 4 and above)

Syntax:

Parameter:

Name Description Required /
Optional
Type
var_name The variable being checked Required Mixed*

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value:

TRUE if var is an array, FALSE otherwise.

Value Type: Boolean.

Example :

Practice here online :

See also

Previous: intval
Next: is_bool

Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

Di?erence between __FUNCTION__ and __METHOD__

__FUNCTION__ returns only the name of the function whereas __METHOD__ returns the name of the class along with the name of the function:

  • Weekly Trends

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook

How to check if variable is array. or something array-like

I want to use a foreach loop with a variable, but this variable can be many different types, NULL for example.

So before foreach I test it:

But I realized that it can also be a class that implements Iterator interface. Maybe I am blind but how to check whether the class implements interface? Is there something like is_a function or inherits operator? I found class_implements , I can use it, but maybe there is something simpler?

And second, more important, I suppose this function exist, would be enough to check if the variable is_array or «implements Iterator interface» or should I test for something more?

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *