【发布时间】:2019-04-14 08:04:10
【问题描述】:
我通过继承 PHP 的 ArrayObject 类来实现一个类。但是我没有找到如何在迭代时获取数组的索引。我想同时显示键和值
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();
谁能帮帮我?
【问题讨论】:
-
当然你也不想使用手册。 php.net/manual/en/iterator.key.php
-
谢谢兄弟,它正在工作@u_mulder
标签: php arrayobject