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 に答える