用php分类内部的日期foreach循环



我正在尝试构建一个函数,该函数以php的循环中外部JSON feed显示评论。除了排序外,一切都很好。

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

<?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++; } ?>

通常在这些情况下,我发现最好在显示之前组织数据。下面,我通过设置1个foreach来处理"逻辑"和1个foreach来处理实际显示数据。

来完成此操作。

从这里开始,它与添加自定义排序算法一样简单。(有关更深入的示例,请参见按子阵列值进行php排序数组)

<?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++; } ?>

也许这样的东西:

$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
}

谢谢大家的解释。我仍然是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++; } ?>

最新更新