3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ ['id' => 1, 'city' => 'Melbourne', 'user_name' => 'user_1'], ['id' => 2, 'city' => 'Melbourne', 'user_name' => 'user_1'], ['id' => 3, 'city' => 'Sydney', 'user_name' => 'user_2'], ['id' => 4, 'city' => 'Perth', 'user_name' => 'user_3'], ['id' => 5, 'city' => 'Perth', 'user_name' => 'user_4'] ]; var_export( array_map( 'count', array_reduce( $array, function($result, $row) { $result[$row['city']][$row['user_name']] = true; return $result; } ) ) );

preferences:
24.59 ms | 406 KiB | 5 Q