-
-
Save victorknust/11297372 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Class TableRII extends RecursiveIteratorIterator { | |
private $parentKey = null; | |
public function beginIteration() { | |
echo '<table border=1><tr><th>key</th><th>value</th></tr>'; | |
} | |
public function endIteration() { | |
echo '</table>'; | |
} | |
public function beginChildren() { | |
echo '<tr><td>'. $this->parentKey .'</td><td><table border=1>'; | |
} | |
public function endChildren() { | |
echo '</table></td></tr>'; | |
} | |
public function callHasChildren() { | |
$bool = parent::callHasChildren(); | |
if($bool) { | |
$this->parentKey = $this->key(); | |
} | |
return $bool; | |
} | |
public function current() { | |
return '<tr><td>'. $this->key() .'</td><td>'. parent::current() . '</td></tr>'; | |
} | |
} | |
/*-------main----------*/ | |
$array = array( | |
array('Post' => array('id' => 1, 'title' => 'aa1')), | |
array('Post' => array('id' => 2, 'title' => 'aa2')), | |
array('Post' => array('id' => 3, 'title' => 'aa3')), | |
); | |
$Iterator = new TableRII(new RecursiveArrayIterator($array), RecursiveIteratorIterator::LEAVES_ONLY); | |
foreach ($Iterator as $k => $v) { | |
if(!is_array($v)){ | |
echo $v. "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment