How to find the difference between the PHP Object array?

Source: Internet
Author: User
The following code is available on Page 159 of "Deep PHP object-oriented, patterns, and practices. Here is a demonstration of the combination mode. The array_udiff function is used in the removeUnit method. The author intends to remove the $ unit object from the $ units attribute. I tried to find that it didn't work. The key is to have the following code on Page 159 of "Deep PHP object-oriented, patterns and practices.
Here is a demonstration of the combination mode. The array_udiff function is used in the removeUnit method. The author intends to remove the $ unit object from the $ units attribute. I tried to find that it does not work. The key is to store objects in the $ units parameter. Objects cannot be sorted during comparison, so they fail. Is there a good way to exclude an object from the array?


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);

Reply content:

The following code is available on Page 159 of "Deep PHP object-oriented, patterns, and practices.
Here is a demonstration of the combination mode. The array_udiff function is used in the removeUnit method. The author intends to remove the $ unit object from the $ units attribute. I tried to find that it does not work. The key is to store objects in the $ units parameter. Objects cannot be sorted during comparison, so they fail. Is there a good way to exclude an object from the array?


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);

The direct loop and unset are solved.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.