3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Extract values from an array, and return the results * * @param array $array The array to filter * @param callable|string $callback Function to filter the array with * * @return array A new array containing the filtered elements */ function array_excise(array &$array, callable|string $callback): array { $result = []; if (!is_callable($callback)) { return $result; } foreach ($array as $key => $value) { if ($callback($value)) { $result[$key] = $value; unset($array[$key]); } } return $result; } $people = [ ["name" => "Billy Jean", "filter" => false], ["name" => "Ronald Reagan", "filter" => true], ["name" => "Bill Clinton", "filter" => true], ["name" => "Michael Jackson", "filter" => false], ["name" => "Johnny Cash", "filter" => false], ]; $presidents = array_excise($people, function($p){ return $p["filter"]; }); var_export(compact(['people', 'presidents'])); echo "\n---\n"; $numbers = [ [1, 1, 1], [1, 1, 0], [1, 0, 0], [0, 0, 0] ]; $noZeros = array_excise($numbers, 'array_product'); var_export(compact(['numbers', 'noZeros']));
Output for git.master, git.master_jit, rfc.property-hooks
array ( 'people' => array ( 0 => array ( 'name' => 'Billy Jean', 'filter' => false, ), 3 => array ( 'name' => 'Michael Jackson', 'filter' => false, ), 4 => array ( 'name' => 'Johnny Cash', 'filter' => false, ), ), 'presidents' => array ( 1 => array ( 'name' => 'Ronald Reagan', 'filter' => true, ), 2 => array ( 'name' => 'Bill Clinton', 'filter' => true, ), ), ) --- array ( 'numbers' => array ( 1 => array ( 0 => 1, 1 => 1, 2 => 0, ), 2 => array ( 0 => 1, 1 => 0, 2 => 0, ), 3 => array ( 0 => 0, 1 => 0, 2 => 0, ), ), 'noZeros' => array ( 0 => array ( 0 => 1, 1 => 1, 2 => 1, ), ), )

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:
69.81 ms | 407 KiB | 5 Q