0bc11.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 35 行
PHP
35 行
<?php// Include the library for Key Iteration//require 'KeyArrayIterator.php';require '0Bc07.php';// Declare a new class that will extend our previous one, and implement// the ability to do recursionclass RecursiveKeyArrayIterator extends KeyArrayIterator implements RecursiveIterator { // We need to define two methods to accomlish this. First of all // Define a function to check if we happen to have 'children' public function hasChildren() { // In this case, children refers to this element being an array. return is_array($this->current()->value); } // Now we also need to declare a method to return the children public function getChildren() { // The way to actually access the children needs to be via an // Iterator, therefore just define a new Iterator here: return new RecursiveKeyArrayIterator($this->current()->value); }}// That was really all we had to do. So to test this let's make an array$arr = array(42, array(1973, 1974, 2005), 173, array('Tash', 'Livia'), 'a');// Create the Iterator for us to use:$it = new RecursiveIteratorIterator(new RecursiveKeyArrayIterator($arr));// Now loop over the array. echoing it out with keys & Depth!foreach ($it as $entry) { echo $it->getDepth(), ' - ', $entry->key, ' - ', $entry, "<br />\n";}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?