Skip to content

Instantly share code, notes, and snippets.

@tsukkee
Created June 10, 2009 16:43
Show Gist options
  • Save tsukkee/127336 to your computer and use it in GitHub Desktop.
Save tsukkee/127336 to your computer and use it in GitHub Desktop.
<?php
function array_unique_callback($array, $callback) {
return array_intersect_key($array, array_unique(array_map($callback, $array)));
}
function book_compare_func($book) {
return $book->get_title();
}
class Book
{
private $title;
private $category;
private $date;
function __construct($title, $category, $date) {
$this->title = $title;
$this->category = $category;
$this->date = $date;
}
function get_title() {
return $this->title;
}
}
$a = new Book('A', 'aaa', '111');
$b = new Book('B', 'bbb', '222');
$c = new Book('C', 'ccc', '333');
$d = new Book('D', 'ddd', '444');
$book_list = array($a, $d, $a, $c, $b, $c, $c, $d);
$result = array_unique_callback($book_list, 'book_compare_func');
var_dump($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment