Created
May 25, 2016 08:56
-
-
Save tspycher/6a8846b306a7d5b1709374880f36bcb2 to your computer and use it in GitHub Desktop.
array_diff() example with objects instead of primitives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class X { | |
public $name; | |
public function __construct($name) { | |
$this->name = $name; | |
} | |
# This must be defined in order to have array_diff() working | |
#public function __toString() { | |
# return $this->name; | |
#} | |
} | |
$a = new X("A"); | |
$b = new X("B"); | |
$c = new X("C"); | |
$x = array($a, $b, $c); | |
$y = array($b); | |
$z = array_diff($x, $y); # THIS doesn't work! -> Object of class X could not be converted to string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment