dqyuipw44576 2010-12-12 01:02
浏览 33
已采纳

PHP - usort正在修改数组中对象的内容,我该如何防止这种情况?

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've found that some of the values of the objects have changed along with their position in the array. What am I missing? I don't believe there are any side effects in my user comparison function. Does usort deconstruct/reconstruct objects some how?

Here is the the user comparison function I'm using:

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'm using look like this :

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 usort is run, every object has a valid float value. After usort, two of them are NaN.

RESOLUTION:

date_diff has side effects -- at least in my build of PHP. Removing it fixed the issues entirely.

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;   
    }
}
  • 写回答

2条回答 默认 最新

  • drm16022 2010-12-12 01:35
    关注

    Off the top of my head, I don't see anything in there that should be modifying the contents of the array elements themselves.

    The way usort() works is that when it determines the new order of the elements, it adds new elements to the array which holds the value of the originals, then removes the originals, so strictly speaking, none of the elements of the array you passed into the function ever come out, only copies of them do (albeit as part of the same array variable you passed in).

    I don't see any reason why usort would destruct and re-construct objects, as you asked, but it does do a similar thing to the array itself. As far as I can tell, nothing in the value of the array elements should be changed, unless you do it explicitly in the comparison function.

    In your CompareSegments method, you make calls to a StartDateTime->GetString() method. Is it possible that the GetString() method is modifying your data?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流