0

我正在构建一个从数据库中获取的“嵌套”数组;这是我的脚本:

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)

请问,有什么帮助吗?提前致谢

4

2 回答 2

2

自己数就行了:

$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);
}
于 2012-04-14T08:07:50.083 回答
0

感谢datacompboy,我终于明白了:

while ($row_rsMaster = mysql_fetch_assoc($rsMaster)) {
    $counter = count($numbers)+1;
    $numbers[] = array("Page " . $counter, $counter);
}
于 2012-04-14T09:07:56.280 回答