Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按日期对JSON日期排序-PHP_Php_Json_Date - Fatal编程技术网

按日期对JSON日期排序-PHP

按日期对JSON日期排序-PHP,php,json,date,Php,Json,Date,我有一个网站在收集汇流博客的数据。我使用的API似乎没有排序功能。谁能帮我得到PHP排序的JSON日期页上的文章,我需要最新的第一。谢谢 <?php require_once($_SERVER["DOCUMENT_ROOT"].'/layout/layout.inc.php'); require_once($_SERVER["DOCUMENT_ROOT"].'/functions/general.inc.php'); $layout = new my_layout(); $layout

我有一个网站在收集汇流博客的数据。我使用的API似乎没有排序功能。谁能帮我得到PHP排序的JSON日期页上的文章,我需要最新的第一。谢谢

<?php
require_once($_SERVER["DOCUMENT_ROOT"].'/layout/layout.inc.php');
require_once($_SERVER["DOCUMENT_ROOT"].'/functions/general.inc.php');

$layout = new my_layout();

$layout->title('IT KB Search');

$layout->content("<div class='border'>");
$layout->content('<h1>IT Support Knowledge Base - Search Results</h1>');

    $baseUrl = 'https://website.atlassian.net/wiki';
    $url = $baseUrl.'/rest/api/content?spaceKey=KB&type=blogpost&start=0&limit=10&expand=space,history,body.view,metadata.labels';

    // To enable authenticated search: 
    // $url .= "&os_username=$username&os_password=$password";

    $response = file_get_contents($url);
    $response = json_decode($response);
    $results = $response->results;

    $html .= '<dl style=list-style: none;>';
    foreach($results as $item) {
        $date = $item-> history-> createdDate;
        $html .= '<strong><a href="';
        $html .= $baseUrl. $item-> _links-> webui;
        $html .= '" target="_blank">';
        $html .= $item->title;
        $html .= ' - ';
        $html .= date("d/m/Y",strtotime($date));
        $html .= '</a></strong><br>';
        $html .= $item->body-> view-> value;
        $html .= '<br>';
    }
    $html .= '</dl>';

    $layout->content($html);

$layout->content('</div>');
$layout->render();

您可以尝试以下方法:-

function date_compare($a, $b)
{
    $t1 = strtotime($a['datetime']);
    $t2 = strtotime($b['datetime']);
    return $t1 - $t2;
}    
usort($results, 'date_compare');
您可以在php文件中声明函数date_compare,并使用以下语句:-

usort($results, 'date_compare');

在for each循环之前,您可以尝试以下方法:-

function date_compare($a, $b)
{
    $t1 = strtotime($a['datetime']);
    $t2 = strtotime($b['datetime']);
    return $t1 - $t2;
}    
usort($results, 'date_compare');
您可以在php文件中声明函数date_compare,并使用以下语句:-

usort($results, 'date_compare');

就在for each循环之前

foreach循环之前,您可以像这样使用
usort

usort($results, function ($a, $b) {
    return strtotime($a->history->createdDate) - strtotime($b->history-> createdDate);
});

一旦完成,$result将按排序顺序提供数据$a-$b将按升序提供数据,$b-$a将按降序提供数据foreach循环之前,您可以这样使用
usort

usort($results, function ($a, $b) {
    return strtotime($a->history->createdDate) - strtotime($b->history-> createdDate);
});

一旦完成,$result将按排序顺序提供数据$a-$b将按升序提供数据,$b-$a将按降序提供数据

如果这是一个noob问题,但是我如何将其合并到我的代码中,使a和b成为我的数据?如果这是一个noob问题,很抱歉,但是我如何将其合并到我的代码中,使a和b成为我的数据?