3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); $array = array(array('id' => 1, 'fid' => 2), array('id' => 2, 'fid' => 2)); class Map { public static function byProperty(array $array, $property) { $map = array(); foreach ($array as $record) { $propValue = self::_getPropertyValue($record, $property); $map[$propValue][] = $record; } return $map; } public static function byMultiProperty(array $array, array $properties) { $map = array(); $lastProperty = end($property); foreach($array as $record) { $pointer = $map; foreach($properties as $property) { $propValue = self::_getPropertyValue($record, $property); if(!isset($pointer[$propValue])) { $pointer[$propValue] = array(); } $pointer = &$pointer[$propValue]; if($property === $lastProperty) { $pointer[] = $record; } } } return $map; } /** * @param mixed $record * @param string $property * @return mixed */ private static function _getPropertyValue($record, $property) { if (is_array($record)) { return isset($record[$property]) ? $record[$property] : null; } if (is_object($record)) { return isset($record->$property) ? $record->$property : null; } throw new InvalidArgumentException(sprintf('Unsupport record type: %s', gettype($record))); } } var_dump(Map::byMultiProperty($array, array('fid', 'id')));
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught TypeError: end(): Argument #1 ($array) must be of type array, null given in /in/hsA2o:21 Stack trace: #0 /in/hsA2o(21): end(NULL) #1 /in/hsA2o(59): Map::byMultiProperty(Array, Array) #2 {main} thrown in /in/hsA2o on line 21
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:
58.11 ms | 401 KiB | 8 Q