3v4l.org

run code in 300+ PHP versions simultaneously
<?php $users = [ ['email' => 'oldemail@email.com', 'state' => 'IL'], ['email' => 'diditwork@email.com', 'state' => 'HI'], ]; $jobs = [ ['title' => 'Marketing Coordinator', 'location' => 'Chicago', 'state' => 'IL'], ['title' => 'Sales Manager (MA)', 'location' => 'Springfield', 'state' => 'IL'], ['title' => 'Security Guard/Driver', 'location' => 'Big Island', 'state' => 'HI'], ['title' => 'Directory of Sales and Operation Planning', 'location' => 'Honolulu', 'state' => 'HI'], ]; foreach ($jobs as $job) { $jobs_by_state[$job['state']][] = $job; } foreach ($users as &$user) { $user['jobs'] = $jobs_by_state[$user['state']]; } echo json_encode($users, JSON_PRETTY_PRINT);
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.26, 8.0.0 - 8.0.13, 8.1rc1 - rc3
[ { "email": "oldemail@email.com", "state": "IL", "jobs": [ { "title": "Marketing Coordinator", "location": "Chicago", "state": "IL" }, { "title": "Sales Manager (MA)", "location": "Springfield", "state": "IL" } ] }, { "email": "diditwork@email.com", "state": "HI", "jobs": [ { "title": "Security Guard\/Driver", "location": "Big Island", "state": "HI" }, { "title": "Directory of Sales and Operation Planning", "location": "Honolulu", "state": "HI" } ] } ]

preferences:
36.21 ms | 462 KiB | 5 Q