1

请一些新鲜的眼睛告诉我我在这里做错了什么?我确定这很简单。

检索数据集的sql查询:

SELECT * FROM articles WHERE category='category' ORDER BY publish_date DESC, id DESC LIMIT 0, 10

我试图达到的多维数组:

list [ 
    publish_date [
        0 [ id, title, body ]
        1 [ id, title, body ]
        2 [ id, title, body ]]
    publish_date [
        0 [ id, title, body ]
        1 [ id, title, body ]]
    publish_date [
        0 [ id, title, body ]
        1 [ id, title, body ]
        2 [ id, title, body ]
        3 [ id, title, body ]]]
Etc..

几个小时以来,我一直在摆弄以下 php 函数:

$result = $sql->prepareQuery($query_string);

// iterate through array and place results in an array at row index
$list = ['group_date' => ' '];
$r = 0;
while ($rows = mysql_fetch_assoc($result)) {
    if ($list['publish_date'] != $rows['pub_date']) {
        $list['publish_date'] = $rows['pub_date'];
        $r = 0;
        foreach ($rows as $key => $val) {
            $list['publish_date'][$r[$key = $val]];
        }
        $r++;
    }
    else {
        foreach ($rows as $key => $val) {
            $list['publish_date'][$r[$key = $val]];
        }
        $r++;
    }
}
// return result
print_r($list);
4

1 回答 1

1

认为您正在使事情变得复杂:

$list = array();
$current_date = '';
while ($rows = mysql_fetch_assoc($result)) {
    if ($current_date != $rows['pub_date']) {
        $current_date = $rows['pub_date'];
        $list[$current_date] = array();
    }
    $list[$current_date][] = $rows;
}
于 2013-01-09T18:49:33.750 回答