3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $selected_emails = array( 16781 => "xxx@gmail.com", 16782 => "yyy@gmail.com", 16785 => "xxx@gmail.com,yyy@gmail.com", 16789 => "yyy@gmail.com", 16751 => "xxx@gmail.com,yyy@gmail.com,zzz@gmail.com", 16729 => "zzz@gmail.com,bbb@gmail.com" ); $final_array = array(); foreach ($selected_emails as $newsletter => $emails) { foreach (explode(',', $emails) as $email) { $final_array[$email][] = $newsletter; } } var_dump($final_array);
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(4) { ["xxx@gmail.com"]=> array(3) { [0]=> int(16781) [1]=> int(16785) [2]=> int(16751) } ["yyy@gmail.com"]=> array(4) { [0]=> int(16782) [1]=> int(16785) [2]=> int(16789) [3]=> int(16751) } ["zzz@gmail.com"]=> array(2) { [0]=> int(16751) [1]=> int(16729) } ["bbb@gmail.com"]=> array(1) { [0]=> int(16729) } }