所以我正在创建这个网络应用程序,我需要从多个社交网络网站获取数据,并根据帖子创建日期和时间对它们进行排序,我需要将所有提要合并到一个数组中,然后再显示它们。
所以这是我到目前为止所做的:
- >从twitter,facebook,Instagram获取数据
- >包含所有数据的3个不同数组。
现在我需要想办法对它们进行排序并将它们合并到一个数组中....
写了这个函数,但需要时间:
function sortNjoin($arr1, $arr2)
{
$length = count($arr1);
$sortedArray = array();
if(count($arr1)>count($arr2))
{
$length = count($arr2);
}
$date1 ='';
$date2 = '';
for($i=0;$i<$length;$i++)
{
for($j=0;$j<$length;$j++)
{
switch($arr1['type'])
{
case 'f':
$date1 = date_parse($arr1['dataInfo'][$i]['updated_time']);
break;
case 't':
$date1 = date_parse($arr1['dataInfo'][$i]['created_at']);
break;
case 'i':
$date1 = date_parse($arr1['dataInfo'][$i]['external_created_at']);
break;
}
switch($arr2['type'])
{
case 'f':
$date2 = date_parse($arr2['dataInfo'][$j]['updated_time']);
break;
case 't':
$date2 = date_parse($arr2['dataInfo'][$j]['created_at']);
break;
case 'i':
$date2 = date_parse($arr2['dataInfo'][$j]['external_created_at']);
break;
}
$date_string1 = date('Y-m-d H:i:s', mktime($date1['hour'], $date1['minute'], $date1['second'], $date1['month'], $date1['day'], $date1['year']));
$date_string2 = date('Y-m-d H:i:s', mktime($date2['hour'], $date2['minute'], $date2['second'], $date2['month'], $date2['day'], $date2['year']));
if($date_string1 >= $date_string2)
{
$sortedArray[$i]['dataInfo'] = $arr1['dataInfo'][$i];
$sortedArray[$i]['type'] = $arr1['type'];
}
else
{
$sortedArray[$i]['dataInfo'] = $arr2['dataInfo'][$j];
$sortedArray[$i]['type'] = $arr2['type'];
}
}
}
return $sortedArray;
}
答案 0 :(得分:0)
看看usort。这允许您使用用户定义的函数对数组进行排序。
答案 1 :(得分:0)
<强> UDPATE 强>
注意:我有用户Facebook
&amp; Twitter
数据
按值按降序排序的功能
function sortArr($a, $b){
return $b['date'] - $a['date'];
}
代码
<?php
//json to associative array
$fb = json_decode(@'{"data": [{
"id": "X999_Y999",
"from": {
"name": "Tom Brady", "id": "X12"
},
"message": "Looking forward to 2010!",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
}]}',true);
//twitter json
$twitter = json_decode(@'{"results":[{
"text":"@twitterapi tinyurl.com/ctrefg",
"from_user":"jkoum",
"id":1478555574,
"from_user_id":1833773,
"created_at":"Wed, 08 Apr 2009 19:22:10 +0000"}]
}',true);
//Rename Array's key to common key name
$fb = array_map(function($fb) {
return array(
'id' => $fb['id'],
'from' => $fb['from'],
'message' => $fb['message'],
'date' => $fb['updated_time']
);
}, $fb);
$twitter = array_map(function($twitter {
return array(
'id' => $twitter ['id'],
'from' => $twitter ['from_user'],
'message' => $twitter ['text'],
'date' => $fbtwitter 'updated_time']
);
}, $twitter );
//Now Both Array has same key i.e id,from,message,date and we can merged them
$mergedArr=array_merge($fb,$twitter);
//Now sort the merge array by value of ['date'] and we get the result
usort($mergedArr,"sortArr") // sortArr = function we've written above.
print_r($mergedArr);
?>
注意:我只从JSON中选择了少量数据来减少空间。
!!我希望这可以帮助你!!
答案 2 :(得分:0)
<?php
$foo = array(
array(
'title' => 'Foo One',
'date_info' => array(
'created_at' => '2006-12-12 10:00:30'
)
),
array(
'title' => 'Foo Two',
'date_info' => array(
'created_at' => '2006-12-12 10:23:00'
)
),
array(
'title' => 'Foo Three',
'date_info' => array(
'created_at' => '2006-12-12 10:47:00'
)
)
);
$bar = array(
array(
'title' => 'Bar One',
'date_info' => array(
'created_at' => '2006-12-12 10:00:00'
)
),
array(
'title' => 'Bar Two',
'date_info' => array(
'created_at' => '2006-12-12 10:13:00'
)
),
array(
'title' => 'Bar Three',
'date_info' => array(
'created_at' => '2006-12-12 10:47:30'
)
)
);
$all = array_merge($foo, $bar);
usort($all,
function($a, $b) {
$ts_a = strtotime($a['date_info']['created_at']);
$ts_b = strtotime($b['date_info']['created_at']);
return $ts_a > $ts_b;
}
);
var_export($all);
输出:
array (
0 =>
array (
'title' => 'Bar One',
'date_info' =>
array (
'created_at' => '2006-12-12 10:00:00',
),
),
1 =>
array (
'title' => 'Foo One',
'date_info' =>
array (
'created_at' => '2006-12-12 10:00:30',
),
),
2 =>
array (
'title' => 'Bar Two',
'date_info' =>
array (
'created_at' => '2006-12-12 10:13:00',
),
),
3 =>
array (
'title' => 'Foo Two',
'date_info' =>
array (
'created_at' => '2006-12-12 10:23:00',
),
),
4 =>
array (
'title' => 'Foo Three',
'date_info' =>
array (
'created_at' => '2006-12-12 10:47:00',
),
),
5 =>
array (
'title' => 'Bar Three',
'date_info' =>
array (
'created_at' => '2006-12-12 10:47:30',
),
),
)