0

次のようなデータベース結果を返す関数があります。

<?php print_r($homepagematches; ?> 
Array
(
[0] => Array
    (
        [matchid] => 30
        [matchtitle] => Testing This!
        [matchaverage] => 1
        [matchyoutubecode] => New Match
        [casterid] => 1
        [matchtype] => 
        [matchdate] => 2013-05-24 02:19:49
        [matchcasteryoutube] => http://youtube.com/huskystarcraft
        [matchcaster] => HuskyStarcraft
    )

[1] => Array
    (
        [matchid] => 27
        [matchtitle] => Psy vs Camara
        [matchaverage] => 1
        [matchyoutubecode] => nefC9vkMfG8
        [casterid] => 1
        [matchtype] => 
        [matchdate] => 2013-05-24 02:13:10
        [matchcasteryoutube] => http://youtube.com/huskystarcraft
        [matchcaster] => HuskyStarcraft
    )

この関数は、過去 3 日間のすべての一致を返します。私が理解しようとしているのは、投稿された日の下に一致を表示できるように配列を再構成する方法です。これにはおそらく foreach ループが必要であることはわかっていますが、実装する必要がある概念について理解できません。

$matchdate = '';
 foreach($this->data['homepagematches'] as $match){
    if($matchdate != date('m/d', strtotime($match['matchdate'])) || $matchdate == ''){
    $homematch[date('m/d', strtotime($match['matchdate']))] = array(
    "matchtitle" => $match['matchtitle']);
    }

基本的に、次のような配列が必要です。

Array 
(
[05/24] => Array
        (
              [matchid] =>30
              [matchtitle] => Testing This!
              [matchyoutubecode] => New Match
              [casterid] = 1
         )
 )
4

2 に答える 2

0
<?php
$MatchesByDate = array();
foreach($homepagematches as $match) {
    list($matchdate,$time) = explode(" ",$match["matchdate"]); //split field into date and time
    if( !isset($MatchesByDate[$matchdate]) )
         $MatchesByDate[$matchdate] = array(); // If the array for that date doesnt exist yet make it
    $MatchesByDate[$matchdate][] = $match; //Use the matchdate as a key and add to the array
}
于 2013-05-25T18:40:58.913 に答える
0

これでいいと思います。

foreach($this->data['homepagematches'] as $match){
    $tag = date('m/d', strtotime($match['matchdate']));
    $data[$tag][] = array(
        "matchid" => $match['matchid'],
        "matchtitle" => $match['matchtitle'],
        "matchyoutubecode" => $match['matchyoutubecode'],
        "casterid" => $match['casterid']
    );
}
print_r($data);
于 2013-05-25T18:41:49.603 に答える