PHP - usort modifies the contents of objects in an array, how can I prevent this?

I am using usort with a user comparison function to sort an array of objects. After running usort on an array of these objects, I found that some of the values ​​of the objects have changed along with their position in the array. What am I missing? I do not believe that there are any side effects in my user comparison function. Is there any way to destroy / restore objects?

Here is a custom comparison function that I use:

private function SortArrayOfSegments($segments){ foreach($segments as $segment){ echo '<pre>'; var_dump($segment); } usort($segments, "AirObject::CompareSegments"); foreach($segments as $segment){ var_dump($segment); echo '</pre>'; } return $segments; } public static function CompareSegments($a, $b){ $interval = date_diff(date_create($a->StartDateTime->GetString()), date_create($b->StartDateTime->GetString())); if($interval->invert == 1){ return 1; }else if($interval->y == 0 && $interval->m == 0 && $interval->d == 0 && $interval->i == 0 && $interval->s == 0 && $interval->h == 0){ return 0; }else if($interval->invert == 0){ return -1; } } 

The objects I use are as follows:

 object(AirSegment)#14 (12) { ["StartDateTime"]=> object(VDateTime)#27 (4) { ["date"]=> string(10) "2010-12-07" ["time"]=> string(8) "09:23:21" ["timezone"]=> string(0) "" ["utc_offset"]=> string(0) "" } ["EndDateTime"]=> object(VDateTime)#23 (4) { ["date"]=> string(10) "2010-12-07" ["time"]=> string(8) "13:23:21" ["timezone"]=> string(0) "" ["utc_offset"]=> string(0) "" } ["start_airport_code"]=> string(3) "SFO" ["start_city_name"]=> string(13) "San Francisco" ["end_airport_code"]=> string(3) "STL" ["end_city_name"]=> string(8) "St Louis" ["operating_airline"]=> string(15) "United Airlines" ["operating_flight_number"]=> string(3) "335" ["duration"]=> float(NAN) ["required_properties":protected]=> array(9) { ["StartDateTime"]=> bool(false) ["EndDateTime"]=> bool(false) ["start_airport_code"]=> bool(false) ["end_airport_code"]=> bool(false) ["operating_airline"]=> bool(false) ["operating_flight_number"]=> bool(false) ["start_city_name"]=> bool(false) ["end_city_name"]=> bool(false) ["service_class"]=> bool(true) } ["service_class"]=> string(5) "Coach" ["trip_id"]=> string(4) "1621" } 

The property that is changing is the duration property. Before running usort, each object has a valid float value. After usort, two of them are NaN.

Decision:

date_diff has side effects - at least in my PHP build. Fixing the problem completely fixed the problem.

 public static function CompareSegments($a, $b){ $adate = new DateTime($a->StartDateTime->GetString()); $bdate = new DateTime($b->StartDateTime->GetString()); $lessThan = $adate < $bdate; $equal = $adate == $bdate; $greaterThan = $adate > $bdate; if($lessThan){ return -1; }else if($equal){ return 0; }else{ return 1; } } 
+6
sorting php usort
source share
2 answers

At the top of my head, I see nothing where I need to modify the contents of the elements of the array themselves.

The usort() method is that when it defines a new order of elements, it adds new elements to the array that contains the value of the originals, and then deletes the originals, so strictly speaking, not one of the elements of the array that you passed to the function , did not appear, only copies of them (although they are part of the same array variable to which you passed).

I don’t see the reasons why usort destroyed and reconstructed objects as you requested, but it does a similar thing for the array itself. As far as I can tell, nothing in the value of the elements of the array should change unless you explicitly do so in the comparison function.

In your CompareSegments method CompareSegments you call the StartDateTime->GetString() method. Is it possible that the GetString() method is GetString() your data?

+1
source share

What do the objects look like before using the usort method or any method? Without knowing the initial state of objects, it is difficult to be convincing, but NaN is an erroneous result created by a function passed to usort.

0
source share

All Articles