Jeroen
Jeroen

Reputation: 167

how to decompose a array in PHP and 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 :)

Upvotes: 0

Views: 482

Answers (1)

lpg
lpg

Reputation: 4937

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';
}

Upvotes: 2

Related Questions