Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two array and order this new array by date

Tags:

arrays

php

. I have two array

First One

 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
            )

    )

The second

Array
(
    [0] => Array
        (
            [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
                )

        )

So .... I need to combine my first array with the second and I want to order this new array by date !... Can someone can help me on hint me for doing this ? Thx !

like image 499
user1029834 Avatar asked Jan 31 '12 20:01

user1029834


People also ask

How do I combine two arrays into a new array?

The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.

How do I combine two arrays of elements?

In order to combine (concatenate) two arrays, we find its length stored in aLen and bLen respectively. Then, we create a new integer array result with length aLen + bLen . Now, in order to combine both, we copy each element in both arrays to result by using arraycopy() function.

Can we merge two arrays?

To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve each element from an array and insert(using the array push() method) to another array. Now, we can call the merge() function and pass two arrays as the arguments for merging.


2 Answers

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');
like image 112
Shiplu Mokaddim Avatar answered Oct 05 '22 16:10

Shiplu Mokaddim


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:

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');
like image 31
Ben Carey Avatar answered Oct 05 '22 14:10

Ben Carey