note 113421 added to function.is-array
| From: | joksnet at gmail dot com | Date: | Tue, 08 Oct 2013 22:17:40 +0000 |
| Subject: | note 113421 added to function.is-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196503@lists.php.net to get a copy of this message | ||
Here is something for compare
is_assoc functions.
<?php
function is_assoc1($var)
{
return is_array($var) && array_diff_key($var, array_keys(array_keys($var)));
}
function is_assoc2($var)
{
return is_array($var) && array_diff_key($var, array_fill(0, count($var), null));
}
$count = 1000000;
$a = array('a' => 'foo', 'b' => 1, 'c' => true);
$b = array('a', 'b', 'c');
$i = $start_time = $end_time = 0;
$start_time = microtime(true);
for ($i = 0; $i < $count; $i++) {
assert(is_assoc1($a) === true);
assert(is_assoc1($b) === false);
}
$end_time = microtime(true);
echo 'is_assoc1 : ' . ($end_time - $start_time) . "\n";
$start_time = microtime(true);
for ($i = 0; $i < $count; $i++) {
assert(is_assoc2($a) === true);
assert(is_assoc2($b) === false);
}
$end_time = microtime(true);
echo 'is_assoc2 : ' . ($end_time - $start_time) . "\n";
?>
It prints something like this:
```
% php is_assoc.php
is_assoc1 : 19.36315202713
is_assoc2 : 18.954080104828
```
The first one is from
skaimauve (http://www.php.net/manual/en/function.is-array.php#89332). The second one is mine.
----
Server IP: 69.147.83.199
Probable Submitter: 80.13.96.236
----
Manual Page -- http://php.net/manual/en/function.is-array.php
Edit -- https://master.php.net/note/edit/113421
Del: integrated -- https://master.php.net/note/delete/113421/integrated
Del: useless -- https://master.php.net/note/delete/113421/useless
Del: bad code -- https://master.php.net/note/delete/113421/bad+code
Del: spam -- https://master.php.net/note/delete/113421/spam
Del: non-english -- https://master.php.net/note/delete/113421/non-english
Del: in docs -- https://master.php.net/note/delete/113421/in+docs
Del: other reasons-- https://master.php.net/note/delete/113421
Reject -- https://master.php.net/note/reject/113421
Search -- https://master.php.net/manage/user-notes.php