3v4l.org

run code in 300+ PHP versions simultaneously
<?php function filterTagsByTypes(array $tags, array $types, $count = 1) { $returnTags = []; sort($types); //Sort from low to high => high priority to low priority usort($tags, function($a, $b) { if ($a->type == $b->type) { return 0; } return $a->type > $b->type ? 1 : -1; }); //Sort tags array from low type to high type. foreach ($types as $type) { foreach ($tags as $tag) { //In this case, since both arrays are sorted, the priority //you are looking for will not arrive. if ($tag->type > $type) { break; //So break out and continue to the next type. } if ($tag->type == $type) { $returnTags[] = $tag; } if (count($returnTags) >= $count) { //End it now! return $returnTags; } } } } // Usage: class Tag { private $type; function __construct($type) { $this->type = $type; } } $tags = [ new Tag(50), new Tag(1), new Tag(25) ]; $types = [1, 50]; var_dump(filterTagsByTypes($tags, $types));
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught Error: Cannot access private property Tag::$type in /in/99BBI:7 Stack trace: #0 [internal function]: {closure}(Object(Tag), Object(Tag)) #1 /in/99BBI(6): usort(Array, Object(Closure)) #2 /in/99BBI(46): filterTagsByTypes(Array, Array) #3 {main} thrown in /in/99BBI on line 7
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
36.05 ms | 401 KiB | 8 Q