用PHP对foreach循环内的日期进行排序

时间:2019-03-14 18:38:51

标签: php sorting

我正在尝试构建一个函数,以使用php循环显示来自外部json提要的评论。除排序外,其他一切正常。

我想根据日期对项目进行排序。

<?php
$json_url = get_field('review_json_url', 'option'); // path to your JSON file
$data = file_get_contents($json_url); // put the contents of the file into a variable
$reviews = json_decode($data); // decode the JSON feed  
?>

<?php 
$count=0;               
foreach ($reviews->beoordelingen as $review) if ($count < 10) {
// vars
$publish_date = $review->date_created;
$name = $review->klant_naam;
$title = $review->title;
$content = $review->tekst;
$date = new DateTime($publish_date);
?>
<div class="review">
    <h3 class="name"><?php echo $name; ?></h3>
    <h5 class="location"><?php echo $date->format('d-m-Y'); ?></h5>
    <h4 class="subtitle">"<?php echo $title; ?>"</h4>
    <div class="content">
        <p><?php echo $content; ?></p>
    </div>
</div>
<?php $count++; } ?>

3 个答案:

答案 0 :(得分:1)

通常在这些情况下,我发现最好在显示之前组织数据。下面,我通过设置1 foreach处理“逻辑”和设置1 foreach处理实际显示的数据来完成此操作。

从这里开始,就像添加自定义排序算法一样简单。 (有关更深入的示例,请参见PHP Sort Array By SubArray Value

<?php 

$sorted_reviews = array();
foreach ($reviews->beoordelingen as $review) {
    $date             = new DateTime($publish_date);
    $sorted_reviews[] = array(
        'publish_date' => $review->date_created,
        'name'         => $review->klant_naam,
        'title'        => $review->title,
        'content'      => $review->tekst,
        'date'         => $date,
        'sortable'     => $date->getTimestamp(),
    );
}

usort($sorted_reviews, 'sortByOption');
function sortByOption($a, $b) {
    return strcmp($a['sortable'], $b['sortable']);
}

$count=0;
foreach ($sorted_reviews as $review) if ($count < 10) {
?>
<div class="review">
    <h3 class="name"><?php echo $review['name']; ?></h3>
    <h5 class="location"><?php echo $review['date']->format('d-m-Y'); ?></h5>
    <h4 class="subtitle">"<?php echo $review['title']; ?>"</h4>
    <div class="content">
        <p><?php echo $review['content']; ?></p>
    </div>
</div>
<?php $count++; } ?>

答案 1 :(得分:0)

也许是这样的:

$dates = [];
foreach ($reviews->beoordelingen as $i => $review)
    $dates[$i] = $review->date_created;
asort($dates); // this will sort array by values but keep keys attached to values
foreach (array_keys($dates) as $i)
{
    $review = $reviews->beoordelingen[$i];
    // rest of your code
}

编辑:实际上,使用usort()

有更好的方法
function date_compare($a,$b)
{
    return strcmp($a->date_created,$b->date_created);
}
usort($reviews->beoordelingen, 'date_compare');
foreach ($reviews->beoordelingen as $review)
{
    // rest of your code
}

答案 2 :(得分:0)

谢谢大家的解释。我还是php新秀。我尝试了您的代码,但这仍然没有按日期对评论进行降序或升序排列。现在,我在foreach部分添加了“ array_reverse”。那对我来说就成功了。

<?php 

$sorted_reviews = array();
foreach ($reviews->beoordelingen as $review) {
    $sorted_reviews[] = array(
        'name'         => $review->klant_naam,
        'title'        => $review->title,
        'content'      => $review->tekst,
        'id'           => $review->CustorateID,
        'date'         => new DateTime($review->date_created),
    );
}

$count=0;
foreach (array_reverse($sorted_reviews) as $review) if ($count < 10) {
?>
<div class="review" id="<?php echo$review['id']; ?>">
    <h3 class="name"><?php echo $review['name']; ?></h3>
    <h5 class="location"><?php echo $review['date']->format('d-m-Y'); ?></h5>
    <h4 class="subtitle">"<?php echo $review['title']; ?>"</h4>
    <div class="content">
        <p><?php echo $review['content']; ?></p>
    </div>
</div>
<?php $count++; } ?>