Created
June 10, 2009 16:43
-
-
Save tsukkee/127336 to your computer and use it in GitHub Desktop.
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 | |
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