Domanda

I have an array with following structure:

 Array
    (
        [ResultMass] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        //elements and other arrays

                        [FlightsTo] => SimpleXMLElement Object
                            (
                                [Flight] => SimpleXMLElement Object
                                    (
                                        [Company] => 4G
                                        //other elements
                                    )

                            )

                        [FlightsBack] => SimpleXMLElement Object
                            (
                                [Flight] => SimpleXMLElement Object
                                    (
                                        [Company] => 4G
                                        //other elements

                                    )

                            )

                    )
                    //other elements of 'resultmass'
               )

        )

    [Error] => 0
)

I need to sort ResultMass elements on "FlightsTo->Flight->Company" string of this elements. How can i do this by using array_multisort function? Or it can be done by other method? Thanks.

È stato utile?

Soluzione

You can not do that via array_multisort since your data items are not arrays, they are objects. You can use usort() function to set your own order in your data array.

To set nested order in your case, use this sample:

usort($rgData, function($rX, $rY)
{
   if($rX->FlightsTo == $rY->FlightsTo)
   {
      if($rX->Flight == $rY->Flight)
      {
         return $rX->Company<$rY->Company?-1:$rX->Company!=$rY->Company;
      }
      return $rX->Flight<$rY->Flight?-1:1;
   }
   return $rX->FlightsTo<$rY->FlightsTo?-1:1;
});

Altri suggerimenti

you can use usort function with your own callable method. example below

usort(&$array['ResultMass'], function ($a, $b) {
   $a1 = $a->FlightsTo->Flight->Company;
   $b1 = $b->FlightsTo->Flight->Company;
   if ($a1 == $b1) return 0;
   return ($al > $bl) ? +1 : -1;
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top