。我有两个数组
第一个
Array
(
[0] => Array
(
[date] => 2012-01-10
[result] => 65
[name] => Les océans
)
[1] => Array
(
[date] => 2012-01-11
[result] => 75
[name] => Les mers
)
[2] => Array
(
[date] => 2012-01-13
[result] => 66
[name] => Les continents
[type] => Scores
)
)
第二个
Array
(
[0] => Array
(
[date_end] => 2012-01-12
[result] => 60
[name] => Step#1
[type] => Summary
)
)
我希望这是我的最终结果
Array
(
[0] => Array
(
[date] => 2012-01-10
[result] => 65
[name] => Les océans
)
[1] => Array
(
[date] => 2012-01-11
[result] => 75
[name] => Les mers
)
[2] => Array
(
[date_end] => 2012-01-12
[result] => 60
[name] => Step#1
[type] => Summary
)
[3] => Array
(
[date] => 2012-01-13
[result] => 66
[name] => Les continents
[type] => Scores
)
)
所以....我需要将我的第一个数组与第二个数组合并且我想按日期订购这个新数组!...有人可以帮我提示我这样做吗?谢谢!
答案 0 :(得分:18)
array_merge和usort是您的朋友。
function cmp($a, $b){
$ad = strtotime($a['date']);
$bd = strtotime($b['date']);
return ($ad-$bd);
}
$arr = array_merge($array1, $array2);
usort($arr, 'cmp');
答案 1 :(得分:2)
使用array_merge()组合数组,然后使用sort to sort()对它们进行排序,非常简单。你想要一个例子吗?
这应该为你排序:
function dateSort($a,$b){
$dateA = strtotime($a['date']);
$dateB = strtotime($b['date']);
return ($dateA-$dateB);
}
$arrayOne = array(
array(
'date' => '2012-01-10',
'result ' => 65,
'name' => 'Les océans'
),
array(
'date' => '2012-01-11',
'result ' => 75,
'name' => 'Les mers'
),
array(
'date' => '2012-01-13',
'result ' => 66,
'name' => 'Les continents',
'type' => 'Scores'
)
);
$arrayTwo = array(
array(
'date' => '2012-01-12',
'result ' => 60,
'name' => 'Step#1',
'type' => 'Summary'
)
);
// Merge the arrays
$combinedArray = array_merge($arrayOne,$arrayTwo);
// Sort the array using the call back function
usort($combinedArray, 'dateSort');
答案 2 :(得分:1)
array_merge
您的数组然后使用以下代码作为如何对其进行排序的示例。
function sortDate($val1, $val2)
{
if ($val1['date'] == $val2['date']) {
return 0;
}
return (strtotime($val1['date']) < strtotime($val2['date'])) ? -1 : 1;
}
$array = array(
array('date' => '2012-01-10'),
array('date' => '2011-01-10'),
array('date' => '2012-01-01')
);
usort($array, "sortDate");
print_r($array);