PHP / JSON:在一个数组中对两种不同的时间戳格式进行排序

发布于 2024-10-30 20:12:20 字数 522 浏览 1 评论 0原文

我有一个来自 Facebook 和 Twitter 流的 JSON 输出的合并数组,我想将这两个数组排序为一个,但它们提供了两个不同的时间戳:

Facebook: [created_time] => 1299839651 推特:[created_at] => Mon Mar 07 16:33:49 +0000 2011

目前,输出包含来自两个平台的完整 JSON 内容,以 Array ( [data] => Array ( [0] => Array ( [ id] => (Facebook) 并以 [id_str] => 12345678964508161 ) ) (Twitter) 结尾。 中间部分看起来像这样: &limit=25&until=0 ) [0] =>数组 ( [text] => (Facebook -> Twitter). 我不知道如何处理这个问题。感谢您的帮助。

I have a merged array from the JSON output of a Facebook and Twitter stream and I want to sort those two into one but they offer two different timestamps:

Facebook: [created_time] => 1299839651
Twitter: [created_at] => Mon Mar 07 16:33:49 +0000 2011

Currently the output contains the full JSON content from both platforms beginning with Array ( [data] => Array ( [0] => Array ( [id] => (Facebook) and ending with [id_str] => 12345678964508161 ) ) (Twitter).
In the middle part it looks like this: &limit=25&until=0 ) [0] => Array ( [text] => (Facebook -> Twitter).
I am not sure how to handle this. Thanks for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒中人 2024-11-06 20:12:20

您可以使用 strtotime() 将 Twitter 时间戳转换为看起来更像 Facebook 时间戳的时间戳。

然后您只需根据数字时间戳对它们进行排序

You can use strtotime() to convert the Twitter timestamp into one that looks more like the Facebook timestamp.

Then you simply order them based on the numerical timestamp

夏天碎花小短裙 2024-11-06 20:12:20

假设我正确理解你的问题,这应该可行。

<?php

// assume $array is your array of associative arrays with created_time 
// being facebook timestamps, created_at being twitter


// takes as arguments two associative arrays with
// created_time keys to compare
function sort_by_created_time($arr1, $arr2)
{
    if ($arr1['created_time'] == $arr2['created_time'])
    {
        return 0;
    }
    elseif ($arr1['created_time'] > $arr2['created_time'])
    {
        return 1;
    }
    else
    {
        return -1;
    }
}

// go through each array, generate timestamps if needed
foreach ($array as &$row)
{
    if (!isset($row['created_time']))
    {
        // convert date formats to timestamps if we need to
        $row['created_time'] = strtotime($row['created_at']);
    }
}
unset($row);

usort($array, "sort_by_created_time");

This should work, assuming I understood your problem correctly.

<?php

// assume $array is your array of associative arrays with created_time 
// being facebook timestamps, created_at being twitter


// takes as arguments two associative arrays with
// created_time keys to compare
function sort_by_created_time($arr1, $arr2)
{
    if ($arr1['created_time'] == $arr2['created_time'])
    {
        return 0;
    }
    elseif ($arr1['created_time'] > $arr2['created_time'])
    {
        return 1;
    }
    else
    {
        return -1;
    }
}

// go through each array, generate timestamps if needed
foreach ($array as &$row)
{
    if (!isset($row['created_time']))
    {
        // convert date formats to timestamps if we need to
        $row['created_time'] = strtotime($row['created_at']);
    }
}
unset($row);

usort($array, "sort_by_created_time");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文