note 116569 added to recursivearrayiterator.haschildren
| From: | Dominik | Date: | Fri, 23 Jan 2015 11:20:54 +0000 |
| Subject: | note 116569 added to recursivearrayiterator.haschildren | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201505@lists.php.net to get a copy of this message | ||
hasChildren() works as documented here, but not as its name suggests.
The method does not return whether the current entry actually *has* children. It only returns
whether the element's type is one that is able to have children.
This is a bit counterintuitive if you are processing empty arrays.
Example:
<?php
$data = array(
"element one" => array(true),
"element two" => array(),
"element three" => array(true),
);
$i = new RecursiveIteratorIterator(new
RecursiveArrayIterator($data),RecursiveIteratorIterator::SELF_FIRST);
foreach($i as $key => $value)
{
$type = gettype($value);
$depth = $i->getDepth();
if($i->hasChildren()) {
echo "$depth: $key ($type) has children\n";
} else {
echo "$depth: $key ($type) has no children\n";
}
}
?>
Result:
0: element one (array) has children
1: 0 (boolean) has no children
0: element two (array) has children
0: element three (array) has children
1: 0 (boolean) has no children
----
Server IP: 72.52.91.14
Probable Submitter: 77.244.108.76
----
Manual Page -- http://php.net/manual/en/recursivearrayiterator.haschildren.php
Edit -- https://master.php.net/note/edit/116569
Del: integrated -- https://master.php.net/note/delete/116569/integrated
Del: useless -- https://master.php.net/note/delete/116569/useless
Del: bad code -- https://master.php.net/note/delete/116569/bad+code
Del: spam -- https://master.php.net/note/delete/116569/spam
Del: non-english -- https://master.php.net/note/delete/116569/non-english
Del: in docs -- https://master.php.net/note/delete/116569/in+docs
Del: other reasons-- https://master.php.net/note/delete/116569
Reject -- https://master.php.net/note/reject/116569
Search -- https://master.php.net/manage/user-notes.php