I am implementing a class by inheriting PHP's ArrayObject class. But I don't find how to get the index of the array while iterating. I want to show both key and value
class TestArrayObject extends ArrayObject {
public function displayAsTable() {
$iterator = $this->getIterator();
// Table
echo '<table border="1">';
echo '<tr>';
echo '<th>Keys</th><th>Values</th>';
echo '</tr>';
while ($iterator->valid()) {
echo '<tr>';
echo '<td>'.$iterator->current().'</td><td>'.$iterator->current().'</td>';
echo '</tr>';
$iterator->next();
}
echo '</table>';
}
}
$arrFruits = array('Apple','Banana', 'Mango');
$objArr = new TestArrayObject($arrFruits);
$objArr->displayAsTable();
Can anyone help me?