Skip to content

Instantly share code, notes, and snippets.

@tspycher
Created May 25, 2016 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tspycher/6a8846b306a7d5b1709374880f36bcb2 to your computer and use it in GitHub Desktop.
Save tspycher/6a8846b306a7d5b1709374880f36bcb2 to your computer and use it in GitHub Desktop.
array_diff() example with objects instead of primitives
<?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