Combine two array and order this new array by date

后端 未结 3 1776
时光说笑
时光说笑 2021-02-19 13:58

. I have two array

First One

 Array
    (
        [0] => Array
            (
                [date] => 2012-01-10
                [result] => 65         


        
3条回答
  •  死守一世寂寞
    2021-02-19 14:28

    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');
    

提交回复
热议问题