user1029834
user1029834

Reputation: 783

Merge two 2d arrays then sort rows by date value which may been found in one of two columns

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

Answers (4)

Crashspeeder
Crashspeeder

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

Ben Carey
Ben Carey

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

Shiplu Mokaddim
Shiplu Mokaddim

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

mickmackusa
mickmackusa

Reputation: 47934

  1. Your two input arrays have different key names for the column with the date value, so care will need to be taken to check for the appropriate key while sorting.
  2. The date format in both of the arrays is "big endian" so you can compare them as simple strings (and not bother parsing with 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

Related Questions