我正在构建一个从数据库获取的“嵌套”数组;这是我的脚本:
while ($row_rsMaster = mysql_fetch_assoc($rsMaster)) {
$numbers[] = array("Page ");
}
我想获得以下数组(使用 print_r() 函数),但我完全不知道如何获取页码:
Array
(
[0] => Array
(
[0] => Page 1
[1] => 1
)
[1] => Array
(
[0] => Page 2
[1] => 2
)
[2] => Array
(
[0] => Page 3
[1] => 3
)
[3] => Array
(
[0] => Page 4
[1] => 4
)
)
我尝试过:
$numbers[] = array("Pagina " . key($numbers)+1, key($numbers)+1);
但它并没有带来预期的结果(在我看来,它应该获取“父”数组的当前键编号并递增1)。
自己数一下:
$n = 0;
while ($row_rsMaster = mysql_fetch_assoc($rsMaster)) {
$n++;
$numbers[] = array("Page ".$n, $n);
}
或者,在代码中使用
count($numbers)+1
:
while ($row_rsMaster = mysql_fetch_assoc($rsMaster)) {
$numbers[] = array("Page ".(count($numbers)+1), count($numbers)+1);
}
感谢 datacompboy 我终于明白了:
while ($row_rsMaster = mysql_fetch_assoc($rsMaster)) {
$counter = count($numbers)+1;
$numbers[] = array("Page " . $counter, $counter);
}