3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace efiku; class Book { private $name; private $author; private $codeNumber; /** * Book constructor. * @param string $name * @param string $author * @param int $codeNumber */ public function __construct(string $name, string $author, int $codeNumber) { $this->name = $name; $this->author = $author; $this->codeNumber = $codeNumber; } /** * @return string */ public function getName() : string { return $this->name; } /** * @return string */ public function getAuthor() : string { return $this->author; } /** * @return int */ public function getCodeNumber() : int { return $this->codeNumber; } public function __toString() { return $this->name . " --- " . $this->author . " --- " . $this->codeNumber; } } use efiku\Book; $arrayOfBooks = []; // Generowanie tablicy z książkami foreach (range(1, 20) as $number) { $arrayOfBooks["book.{$number}"] = new Book( "PHP {$number}", "Podręcznik profesjonalisty! wydanie:{$number}", mt_rand(1, $number + 1 * 3) ); } // Sortujemy z użyciem wbudowanego mechanizmu asort($arrayOfBooks); foreach ($arrayOfBooks as $book) { echo $book . PHP_EOL; } ///////////////////////////////////////////////////// echo "\nSORTUJEMY Według Naszego mechanizmu\n\n"; uasort($arrayOfBooks, function (Book $first, Book $second) { $firstEditionNumber = (int)substr(strrchr($first->getAuthor(), ":"), 1); $secondEditionNumber = (int)substr(strrchr($second->getAuthor(), ":"), 1); return $firstEditionNumber <=> $secondEditionNumber; }); foreach ($arrayOfBooks as $book) { echo $book . PHP_EOL; }

preferences:
34.6 ms | 402 KiB | 5 Q