如何找出PHP對象數組的差值?

來源:互聯網
上載者:User
《深入PHP 物件導向、模式與實踐》的159頁有下面的代碼。
這兒是示範組合模式,removeUnit那個方法裡用到了array_udiff函數。作者的本意是這樣可以從 $units 屬性裡去除$unit 對象。我試了發現不起作用,關鍵就在$units參數裡存的是對象,對象做比較時不能排序,所以就失敗了。大家有沒有好的方法可以從數組裡排除掉一個對象嗎?


class Army extends Unit{

private $units = array();function addUnit(Unit $unit){    if(in_array($unit, $this->units, true)){        return;    }       $this->units[] = $unit;}   function removeUnit(Unit $unit){    $this->units = array_udiff($this->units, array($unit), function ($a, $b) {        return ($a === $b) ? 0: 1;}); }   function bombardStrength(){    $ret = 0;    foreach($this->units as $unit){        $ret += $unit->bombardStrength();    }       return $ret;}   

}

$army = new Army;

$archer = new Archer();//$archer->addUnit(new Archer);$army->addUnit($archer);$army->addUnit($archer);$army->addUnit(new Archer);$army->addUnit(new LaserCannonUnit);
$army2 = new Army();$army2->addUnit(new Archer);$army2->addUnit($archer);$army2->addUnit($archer);$army2->removeUnit($archer);$army->addUnit($army2);$army->addUnit($army2);print_r($army);echo $army->bombardStrength()."\n";$army->removeUnit($army2);$army->removeUnit($army2);$army->removeUnit($army2);$army->removeUnit($archer);echo $army->bombardStrength();print_r($army);

回複內容:

《深入PHP 物件導向、模式與實踐》的159頁有下面的代碼。
這兒是示範組合模式,removeUnit那個方法裡用到了array_udiff函數。作者的本意是這樣可以從 $units 屬性裡去除$unit 對象。我試了發現不起作用,關鍵就在$units參數裡存的是對象,對象做比較時不能排序,所以就失敗了。大家有沒有好的方法可以從數組裡排除掉一個對象嗎?


class Army extends Unit{

private $units = array();function addUnit(Unit $unit){    if(in_array($unit, $this->units, true)){        return;    }       $this->units[] = $unit;}   function removeUnit(Unit $unit){    $this->units = array_udiff($this->units, array($unit), function ($a, $b) {        return ($a === $b) ? 0: 1;}); }   function bombardStrength(){    $ret = 0;    foreach($this->units as $unit){        $ret += $unit->bombardStrength();    }       return $ret;}   

}

$army = new Army;

$archer = new Archer();//$archer->addUnit(new Archer);$army->addUnit($archer);$army->addUnit($archer);$army->addUnit(new Archer);$army->addUnit(new LaserCannonUnit);
$army2 = new Army();$army2->addUnit(new Archer);$army2->addUnit($archer);$army2->addUnit($archer);$army2->removeUnit($archer);$army->addUnit($army2);$army->addUnit($army2);print_r($army);echo $army->bombardStrength()."\n";$army->removeUnit($army2);$army->removeUnit($army2);$army->removeUnit($army2);$army->removeUnit($archer);echo $army->bombardStrength();print_r($army);

直接迴圈和unset解決了。

foreach($this->units as $k => $u){          if($u === $unit){             unset($this->units[$k]);         }     }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.