3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_group_by($arr, $key) { if (!is_array($arr)) { trigger_error('array_group_by(): The first argument should be an array', E_USER_ERROR); } if (!is_string($key) && !is_int($key) && !is_float($key)) { trigger_error('array_group_by(): The key should be a string or an integer', E_USER_ERROR); } // Load the new array, splitting by the target key $grouped = array(); foreach ($arr as $value) { $grouped[$value[$key]][] = $value; } // Recursively build a nested grouping if more parameters are supplied // Each grouped array value is grouped according to the next sequential key if (func_num_args() > 2) { $args = func_get_args(); foreach ($grouped as $key => $value) { $parms = array_merge(array($value), array_slice($args, 2, func_num_args())); $grouped[$key] = call_user_func_array('array_group_by', $parms); } } return $grouped; } $rows = array( array( 'name' => 'This is apple', 'category' => 'apple', 'first_category' => 'fruit' ), array( 'name' => 'This is orange', 'category' => 'orange', 'first_category' => 'fruit' ), array( 'name' => 'This is pear', 'category' => 'pear', 'first_category' => 'fruit' ), array( 'name' => 'This is carrot', 'category' => 'carrot', 'first_category' => 'vegetable' ), array( 'name' => 'This is tomatoe', 'category' => 'tomatoe', 'first_category' => 'vegetable' ) ); $items = array_group_by($rows, 'first_category'); foreach($items as $key => $value) { echo '<li><strong>' . $key . '</strong></li>'; foreach($value as $row) { echo '<li><a href="/category/'.$row['category'].'">'.$row['category'].'</a></li>'; } }
Output for git.master, git.master_jit, rfc.property-hooks
<li><strong>fruit</strong></li><li><a href="/category/apple">apple</a></li><li><a href="/category/orange">orange</a></li><li><a href="/category/pear">pear</a></li><li><strong>vegetable</strong></li><li><a href="/category/carrot">carrot</a></li><li><a href="/category/tomatoe">tomatoe</a></li>

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