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 回答