Skip to content

Instantly share code, notes, and snippets.

@victorknust
Forked from ichikaway/gist:2768801
Created April 25, 2014 17:38
Show Gist options
  • Save victorknust/11297372 to your computer and use it in GitHub Desktop.
Save victorknust/11297372 to your computer and use it in GitHub Desktop.
<?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