在php数组中安排数据

时间:2014-08-31 20:23:16

标签: php

我正在尝试以下列格式获取数组:

Array
(
    [2014] => Array
        (
            [Aug] => Array
                (
                    [0] => Array
                        (
                            [0] => 49
                            [1] => Third and FInal Post.
                        )
                    [1] => Array
                        (
                            [0] => 47
                            [1] => Hello this is my First Post.
                        )

                )

        )

    [2013] => Array
        (
            [Sep] => Array
                (
                    [0] => Array
                        (
                            [0] => 48
                            [1] => Second Post!
                        )
               )

        )

)

目前我正在使用以下内容。

$query = "select id,heading,date from post order by date desc;";
$navarray=array();
$result = mysqli_query($connection,$query);
while(($row = mysqli_fetch_row($result))
 $navarray[date('Y',strtotime($row['2']))][]=array(date('M',strtotime($row['2'])), array($row[0],$row[1])); 

知道我哪里错了吗?我知道它必须是一个很小的步骤,但我现在无法想到它!

1 个答案:

答案 0 :(得分:0)

严格来说,设置年份和月份变量并不是必需的,但它会让您更容易阅读,更容易看到您将获得哪种输出。

$query = "select id,heading,date from post order by date desc;";
$navarray=array();
$result = mysqli_query($connection,$query);
while(($row = mysqli_fetch_row($result)) {
    $year = date('Y',strtotime($row['2']));
    $month = date('M',strtotime($row['2']);
    $navarray[$year][$month][] = array($row[0],$row[1]));
}
请注意,有一些不确定因素,因为你不能提供你得到的输出,但是很难知道这是否真的有效。