|
 |
is_array (PHP 3, PHP 4, PHP 5) is_array -- Finds whether a variable is an array Описаниеbool is_array ( mixed var )
Finds whether the given variable is an array.
Список параметров
- var
The variable being evaluated.
Возвращаемые значения
Returns TRUE if var is an array,
FALSE otherwise.
Примеры
Пример 1. Check that variable is an array
<?php
$yes = array('this', 'is', 'an array');
echo is_array($yes) ? 'Array' : 'not an Array';
echo "\n";
$no = 'this is a string';
echo is_array($no) ? 'Array' : 'not an Array';
?>
|
Результат выполнения данного примера: |
is_array
rjg4013 at rit dot edu
16-Jul-2006 10:20
To add to the chaos (or perhaps clarification) of these notes, here's another is_associative_array() function that I made and use..
<?php
function is_associative_array( $var ) {
return is_array( $var ) && !is_numeric( implode( array_keys( $var ) ) );
}
?>
I guess with this logic you could do the following too:
<?php
function is_sequential_array( $var ) {
return is_array( $var ) && is_numeric( implode( array_keys( $var ) ) );
}
?>
I never saw a reason to check if it was empty because if it is, the type seems inconsequential.
RJ Gilligan
jupiter at nospam dot com
29-May-2006 11:42
Will check a Multi-Dimentional Array to any specified level. This is a fix to 11/16/05 submission, which would break since you must supply a foreach with an array. Beware recursive functions shouldn't go over 100 deep or could break the memory stack on server.
<?php
function isDeepMultiArray($multiarray, $level = 2) { if (is_array($multiarray)) { if ($level == 1) { return true; } foreach ($multiarray as $array) { if (isDeepMultiArray($array, $level - 1)) { $message = "I'm a multiarray"; return $message; } } } else { return false; }
}
if (isDeepMultiArray(array(array()), 2)); ?>
BTW my notation is consistent with the PEAR manual on coding standards, which is what php.net says to follow. I hope a function like this gets included in PHP6.
jupiter at nospam dot com
29-May-2006 09:15
Simple check for a Multi-Dimentional Array of any depth
<?php
function isMultiArray($multiarray) {
if (is_array($multiarray)) { foreach ($multiarray as $array) { if (is_array($array)) { return true; } } } return false; }
?>
jupiter at nospam dot com
29-May-2006 07:50
Will check a Multi-Dimentional Array to any specified level. This is a fix to 11/16/05 submission, which would break since you must supply a foreach with an array. Beware recursive functions shouldn't go over 100 deep or could break the memory stack on server.
<?php
function isDeepMultiArray($multiarray, $level = 2) { if (is_array($multiarray)) { if ($level == 1) { return true;
} foreach ($multiarray as $array) { if (isDeepMultiArray($array, $level - 1)) { return true;
} } } else { return false;
}
}
?>
BTW my notation is consistent with the PEAR manual on coding standards, which is what php.net says to follow. I hope a function like this gets included in PHP6.
March
30-Mar-2006 06:28
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;
}
?>
mot4h at cs dot virginia dot edu
22-Feb-2006 01:41
I was looking at several of the other examples for testing if an array is associative and they seemed a little limited, or a little bulky. Here's my take on it, I think it's a nice balance:
<?php
function is_associative($array){
if (!is_array($array)) return false;
foreach(array_keys($array) as $key=>$value) {
if ($key != $value) return true;
}
return false;
}
?>
The idea being that the array_keys() function returns a non-associative array, so if the array you fed it wasn't associative, all the key/value pairs in the array of keys should be identical. Otherwise it must have been associative.
junkpost at list dot ru
27-Jan-2006 04:06
There is another example of check to associative array. (See below early versions.)
<?php
funciton is_assoc_array($var) {
return is_array($var) && !empty($var) && !preg_match('/^\d+$/', implode('', array_keys($var)));
}
?>
mmalone at NOSPAM dot vt dot edu
14-Jan-2006 03:40
I was just profiling a rather large php app I have been working on using APD, and noticed that the is_array() function is (apparently) rather inefficient in terms of 'real' time. Not sure why this might be, but it could be a bottleneck in an application where performance is important.
Here is the applicable pprofp output, for those interested:
Real User System secs/ cumm
%Time (excl/cumm) (excl/cumm) (excl/cumm) Calls call s/call Memory Usage Name
--------------------------------- -----------------------------------------------------
36.9 0.76 0.76 0.00 0.00 0.05 0.05 53 0.0143 0.0143 -99704 is_array
ryanyoder at canby dot com
16-Nov-2005 10:25
this is my attempt at writing a function that would check for a multi dimentional array. where $dim is the number of dimentions you are checking for and $array is of course the variable that is being check. i dont know if there's a better way out there, but this is what i do.
function is_multi_array($array,$dim)
{
if(($dim==1)&&(is_array($array)))
{
return 1;
}
foreach($array as $level2)
{
if(is_multi_array($level2,$dim-1))
{
return 1;
}
}
return 0;
}
doker0 at wp dot pl
25-Jul-2005 03:40
This should hack the hack to accept tables
NOTICE! Works only for 1-dim arrays.
if (!function_exists('http_build_query')) {
function http_build_query($formdata, $numeric_prefix = "")
{
$arr = array();
foreach ($formdata as $key => $val)
$arr[] = is_array($val) ? http_build_query($val,$key) :
( $numeric_prefix !='' ? (urlencode($numeric_prefix) . '[' . urlencode($key) . ']')
: urlencode($key)) . "=".urlencode($val) ;
return implode($arr, "&");
}
}
for query '?a[0]=w&b=ggg&a[1]=x&a[2]=y&a[3]=z'
var_export($_GET); shows
array (
'a' =>
array (
0 => 'w',
1 => 'x',
2 => 'y',
3 => 'z',
),
'b' => 'ggg',
)
and
echo (http_build_query($_GET));
shows
a[0]=w&a[1]=x&a[2]=y&a[3]=z&b=ggg
yonman at nsa dot co dot il
04-Apr-2005 01:31
To expand a bit on dan at cain dot sh's comment - is_array will not properly identify Iterator implementing classes.
dan at cain dot sh
10-Dec-2004 05:05
is_array() under PHP 5.0.2 will return FALSE when passed an object descended from the internal class interface ArrayAccess(spl) even though said object behaves as an array would in most instances.
I've found the following user function helpful with my own classes and functions that expect array(s) as arguments, but work fine with objects that behave as an array would.
<?php
function is_array_abled(&$x)
{
return (bool)($x instanceof ArrayAccess or is_array($x));
}
?>
vhermecz at ixpert dot hu
01-Apr-2004 04:58
Mike's function is quite cool, it is just the one, I was searching for. Using range is a great idea! But it's a bit long for me. Here is a shorter version:
function is_assoc_array($var) {
if (!is_array($var)) {
return false;
}
return array_keys($var)!==range(0,sizeof($var)-1);
}
Or, if you don't want to type that much:
function is_assoc($var) {
return is_array($var) && array_keys($var)!==range(0,sizeof($var)-1);
}
mike-php at spam dot emerge2 dot spam dot com
22-Aug-2003 03:20
All arrays in PHP are associative arrays, but it is quite easy to treat an associative array just like it is a sequential array. However, when dealing with XML-RPC, it is necessary to know whether an array is associative or sequential, so I created this function.
It isn't perfect, since an associative array that just happens to have sequential, integer keys starting with 0 will 'look' exactly like a sequential array, and will fool this function.
/****************************************************************
* is_assoc_array tries to decide whether or not a given array *
* is an associative array, or a sequential array. Of course, no *
* such distinction is made by PHP, so it really just tests *
* whether or not a given array could possibly be a sequential *
* array. Since an associative array with sequential, integer *
* keys 'looks' just like a sequential array, this function will *
* be fooled. *
* *
* BUG: Associative arrays with sequential, integer keys 'look' *
* just like sequential arrays, and will be identified as such. *
* *
****************************************************************/
function is_assoc_array( $php_val ) {
if( !is_array( $php_val ) ){
# Neither an associative, nor non-associative array.
return false;
}
$given_keys = array_keys( $php_val );
$non_assoc_keys = range( 0, count( $php_val ) );
if( function_exists( 'array_diff_assoc' ) ) { # PHP > 4.3.0
if( array_diff_assoc( $given_keys, $non_assoc_keys ) ){
return true;
}
else {
return false;
}
}
else {
if( array_diff( $given_keys, $non_assoc_keys ) and array_diff( $non_assoc_keys, $given_keys ) ){
return true;
}
else {
return false;
}
}
}
| |