Reputation: 783
I have two arrays.
$array1 = [
[
'date' => '2012-01-10',
'result' => 65,
'name' => 'Les océans',
],
[
'date' => '2012-01-11',
'result' => 75,
'name' => 'Les mers',
],
[
'date' => '2012-01-13',
'result' => 66,
'name' => 'Les continents',
'type' => 'Scores',
]
];
$array2 = [
[
'date_end' => '2012-01-12',
'result' => 60,
'name' => 'Step#1',
'type' => 'Summary',
]
];
And I want this for my final result:
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
)
)
I need to merge my first array with the second then I want to order this new array by date.
Upvotes: 12
Views: 13308
Reputation: 4311
array_merge
your arrays and then use the following code as an example of how you can sort it.
$array = [
[
'date' => '2012-01-10',
'result' => 65,
'name' => 'Les océans',
],
[
'date' => '2012-01-11',
'result' => 75,
'name' => 'Les mers',
],
[
'date' => '2012-01-13',
'result' => 66,
'name' => 'Les continents',
'type' => 'Scores',
],
[
'date_end' => '2012-01-12',
'result' => 60,
'name' => 'Step#1',
'type' => 'Summary',
]
];
function sortDate($val1, $val2)
{
if ($val1['date'] == $val2['date']) {
return 0;
}
return (strtotime($val1['date']) < strtotime($val2['date'])) ? -1 : 1;
}
usort($array, "sortDate");
print_r($array);
Output: Demo
Warning: Undefined array key "date"
Warning: Undefined array key "date"
Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated
Warning: Undefined array key "date"
Warning: Undefined array key "date"
Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated
Warning: Undefined array key "date"
Warning: Undefined array key "date"
Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated
Array
(
[0] => Array
(
[date_end] => 2012-01-12
[result] => 60
[name] => Step#1
[type] => Summary
)
[1] => Array
(
[date] => 2012-01-10
[result] => 65
[name] => Les océans
)
[2] => Array
(
[date] => 2012-01-11
[result] => 75
[name] => Les mers
)
[3] => Array
(
[date] => 2012-01-13
[result] => 66
[name] => Les continents
[type] => Scores
)
)
Upvotes: 1
Reputation: 16968
Use array_merge() to combine the arrays, and then use sort to sort() to sort them, very simple. Would you like an example?
This should sort it for you: Demonstration
function dateSort($a,$b){
$dateA = strtotime($a['date']);
$dateB = strtotime($b['date']);
return ($dateA-$dateB);
}
$arrayOne = [
[
'date' => '2012-01-10',
'result' => 65,
'name' => 'Les océans',
],
[
'date' => '2012-01-11',
'result' => 75,
'name' => 'Les mers',
],
[
'date' => '2012-01-13',
'result' => 66,
'name' => 'Les continents',
'type' => 'Scores',
]
];
$arrayTwo = [
[
'date_end' => '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');
var_export($combinedArray);
Output:
Warning: Undefined array key "date"
Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated
Warning: Undefined array key "date"
Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated
Warning: Undefined array key "date"
Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated
array (
0 =>
array (
'date_end' => '2012-01-12',
'result' => 60,
'name' => 'Step#1',
'type' => 'Summary',
),
1 =>
array (
'date' => '2012-01-10',
'result' => 65,
'name' => 'Les océans',
),
2 =>
array (
'date' => '2012-01-11',
'result' => 75,
'name' => 'Les mers',
),
3 =>
array (
'date' => '2012-01-13',
'result' => 66,
'name' => 'Les continents',
'type' => 'Scores',
),
)
Upvotes: 3
Reputation: 57660
array_merge and usort is your friend.
function cmp($a, $b){
$ad = strtotime($a['date']);
$bd = strtotime($b['date']);
return ($ad-$bd);
}
$arr = array_merge($array1, $array2);
usort($arr, 'cmp');
Result: (online demo)
Warning: Undefined array key "date"
Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated
Warning: Undefined array key "date"
Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated
Warning: Undefined array key "date"
Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated
array (
0 =>
array (
'date_end' => '2012-01-12',
'result' => 60,
'name' => 'Step#1',
'type' => 'Summary',
),
1 =>
array (
'date' => '2012-01-10',
'result' => 65,
'name' => 'Les océans',
),
2 =>
array (
'date' => '2012-01-11',
'result' => 75,
'name' => 'Les mers',
),
3 =>
array (
'date' => '2012-01-13',
'result' => 66,
'name' => 'Les continents',
'type' => 'Scores',
),
)
Upvotes: 21
Reputation: 47934
strtotime()
or any other date parser).Code: (Demo)
$result = array_merge($array1, $array2);
usort(
$result,
fn($a, $b) => ($a['date'] ?? $a['date_end']) <=> ($b['date'] ?? $b['date_end'])
);
var_export($result);
Output:
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',
),
)
Upvotes: 0