dtnpzghys01643322 2014-07-28 11:22
浏览 49
已采纳

如何在PHP和Laravel中分解数组

I am using Laravels PHP Framework and i ran into the following problem.

i have got the following array:

array(2) { 
[0]=> array(7) { 
    ["createdAt"]=> string(19) "2014-07-24T10:49:49" 
    ["message"]=> string(103) "Helaas geldt dit niet voor status updates, foto's en video's. Er is wel een trucje die ik zelf altij..." 
    ["pageTitle"]=> string(45) "[Facebook Update] Facebook Introduceert Save!" 
    ["pageURL"]=> string(78) "mywebsiteurl" 
    ["authorName"]=> string(16) "Parsifal Tritsch" 
    ["authorProfileURL"]=> string(34) "http://disqus.com/parsifaltritsch/" 
    ["authorAvatar"]=> string(66) "avatarurl" }

[1]=> array(7) { 
    ["createdAt"]=> string(19) "2014-07-22T22:04:30" 
    ["message"]=> string(103) "Ja, dat vind ik een goed idee! Ik heb vaak al naar 'omwegen' gezocht om iets later te kunnen bekijke..."
    ["pageTitle"]=> string(45) "[Facebook Update] Facebook Introduceert Save!" 
    ["pageURL"]=> string(78) "mywebsiteurl" 
    ["authorName"]=> string(8) "Marjanne" 
    ["authorProfileURL"]=> string(0) "" 
    ["authorAvatar"]=> string(50) "avatarurl" } } 

and i tried to get the 'createdAt' of both arrays, and check if they are 3 days in the past or not.

The above array is a print_r of $DQComments When implementing it i use the following code, but i would like the '$past' to be before the foreach.

foreach($DQComments as $comment){
    $henk = date(substr($comment['createdAt'], 0, 10));
    $jan = date("d-m-Y", strtotime($henk));
    $commentdatum = strtotime($jan);
    $past = strtotime(date("d-m-Y", strtotime("3 days ago")));
    if ($commentdatum >= $verleden) {
        echo '<li class="comment-blok col-12">';
        echo '<div class="col-12">';
        echo '<img src="'.$comment['authorAvatar'].'" class="comment-author-profile-img pull-left">';
        echo '<span class="comment-author pull-left">'.'<a href="'.$comment['authorProfileURL'].'" class="comment-author">'.$comment['authorName'].'</a></span>'.'<span class="created_at">&nbsp;om: '.$jan.'</span>';
        echo '</div>';
        echo '<div class="col-12">';
        echo '<div class="comment-message pull-left">'.$comment['message'].'</div>';
        echo '</div>';
        echo '<div class="col-12 pull-left">';
        echo '<a class="comment-link pull-left" href="'.$comment['pageURL'].'">Bekijk bericht</a>';
        echo '</div>';
        echo '</li>'; 

    } else {
        echo 'There are no new comments';
    }
}

How can i use the 'filter' of checking if the comment is -3 days or more before the foreach?? I know it's not the normal way of doing stuff around here the way i am doing now. But i have checked the internet and SO all over and couldnt find what i was looking for. Or i used the wrong search term.. If this is a duplicate, please let me know :)

  • 写回答

1条回答 默认 最新

  • dtyw10299 2014-07-28 11:31
    关注

    In any way you would iterate the array, so maybe there is no need to previously filter your array... Maybe you could improve just a little bit like this:

    $past = strtotime("3 days ago");
    $numComments = 0;
    foreach($DQComments as $comment){
        $henk = strtotime(substr($comment['createdAt'], 0, 10));
        if ($henk >= $past) {
            echo '<li class="comment-blok col-12">';
            echo '<div class="col-12">';
            echo '<img src="'.$comment['authorAvatar'].'" class="comment-author-profile-img pull-left">';
            echo '<span class="comment-author pull-left">'.'<a href="'.$comment['authorProfileURL'].'" class="comment-author">'.$comment['authorName'].'</a></span>'.'<span class="created_at">&nbsp;om: '.$jan.'</span>';
            echo '</div>';
            echo '<div class="col-12">';
            echo '<div class="comment-message pull-left">'.$comment['message'].'</div>';
            echo '</div>';
            echo '<div class="col-12 pull-left">';
            echo '<a class="comment-link pull-left" href="'.$comment['pageURL'].'">Bekijk bericht</a>';
            echo '</div>';
            echo '</li>'; 
            $numComments ++;
        }
    }
    
    if($numComments == 0) {
            echo 'There are no new comments';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题