Sort object(SimpleXMLElement) php

后端 未结 4 1436
北荒
北荒 2020-12-22 04:23

I\'m trying to find a way to sort my array from SimpleXMLElement. I\'d like to sort by start time which I can get from event_start_dt. I\'d also like to sort by room ID as a

4条回答
  •  臣服心动
    2020-12-22 05:11

    Use array_multisort

                foreach ($rez->reservation as $value)
                { 
                         $dateTime[] = $value->event_start_dt;
                }
    
               array_multisort($dateTime,SORT_ASC,SORT_STRING,$rez->reservation);   
               echo "
    ";
               print_r($rez->reservation);
    

    Check it. this is my code

     array
                            (
                                'dateTime' => '2013-12-02T10:00:00-08:00',
                                'chanl1' => '20.10',
                                'chanl2' => '45.4',
                                'chanl3' => '',
                            ),
    
                        1 => array
                            (
                                'dateTime' => '2013-12-02T11:00:00-08:00',
                                'chanl1' => '20.11',
                                'chanl2' => '45.4',
                                'chanl3' => '',
                            ),
                      2 => array
                            (
                                'dateTime' => '2013-12-02T12:00:00-08:00',
                                'chanl1' => '20.12',
                                'chanl2' => '33.8',
                                'chanl3' => '',
                            ),
    
                        3 => array
                            (
                                'dateTime' => '2013-12-02T09:00:00-08:00',
                                'chanl1' => '20.9',
                                'chanl2' => '33.9',
                                'chanl3' => ''
                            ));
    
                foreach($myarray as $c=>$key) {
                        $dateTime[] = $key['dateTime'];                      
                }           
    
    
            array_multisort($dateTime,SORT_ASC,SORT_STRING,$myarray);   
            echo "
    ";
            print_r($myarray);
            ?>
    

    Output is :

    Array
    (
        [0] => Array
            (
                [dateTime] => 2013-12-02T09:00:00-08:00
                [chanl1] => 20.9
                [chanl2] => 33.9
                [chanl3] => 
            )
    
        [1] => Array
            (
                [dateTime] => 2013-12-02T10:00:00-08:00
                [chanl1] => 20.10
                [chanl2] => 45.4
                [chanl3] => 
            )
    
        [2] => Array
            (
                [dateTime] => 2013-12-02T11:00:00-08:00
                [chanl1] => 20.11
                [chanl2] => 45.4
                [chanl3] => 
            )
    
        [3] => Array
            (
                [dateTime] => 2013-12-02T12:00:00-08:00
                [chanl1] => 20.12
                [chanl2] => 33.8
                [chanl3] => 
            )
    
    )
    

    FIDDLE

提交回复
热议问题