3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_transpose(array|object $object_or_array): array|object { $levelOneType = gettype($object_or_array); if (!in_array($levelOneType, ['array', 'object'])) { throw new Exception("Error: array_transpose() expects parameter 1 to be an array or object, $levelOneType given"); } $result = []; foreach ($object_or_array as $rowKey => $row) { $levelTwoType = gettype($row); if (!in_array($levelTwoType, ['array', 'object'])) { throw new Exception("Error: array_transpose() expects parameter 1 to contain rows of arrays or objects, $levelTwoType given"); } foreach ($row as $columnKey => $value) { $result[$columnKey][$rowKey] = $value; } } return $result; } $names = ['Anne', 'Burt', 'Chad', 'Dawn']; $ages = [18, 22, 20, 21]; $pets = ['dog', 'cat', 'fish', 'bird']; var_export(array_transpose(compact(['names', 'ages', 'pets'])));
Output for git.master, git.master_jit, rfc.property-hooks
array ( 0 => array ( 'names' => 'Anne', 'ages' => 18, 'pets' => 'dog', ), 1 => array ( 'names' => 'Burt', 'ages' => 22, 'pets' => 'cat', ), 2 => array ( 'names' => 'Chad', 'ages' => 20, 'pets' => 'fish', ), 3 => array ( 'names' => 'Dawn', 'ages' => 21, 'pets' => 'bird', ), )

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:
28.22 ms | 406 KiB | 5 Q