fork download
  1. <?php
  2.  
  3. $arr = [[
  4. 'forms_email' => 'max@gmail.com, sm@yandex.ru',
  5. 'form_name' => 'form_compl'
  6. ], [
  7. 'forms_email' => 'max@gmail.com, 123@da.fwe',
  8. 'form_name' => 'TESTED'
  9. ], [
  10. 'forms_email' => 'sm@yandex.ru',
  11. 'form_name' => 'formname2'
  12. ]
  13. ];
  14. $tmp = $result = [];
  15.  
  16. foreach($arr as $item){
  17. $subj = trim($item['form_name']); // Получаем имя для текущих мыл
  18. $mails = array_map(function($e){return trim($e);},explode(',', $item['forms_email'])); // Делим мыла на массив и каждый чистим trim'ом
  19.  
  20. foreach($mails as $mail){
  21. if(isset($tmp[$mail]))
  22. if(array_search($subj, $tmp[$mail]) !== false)
  23. continue; // Если уже мыло существует и такая метка у них есть, пропускаем итерацию
  24.  
  25. $tmp[$mail][] = $subj;
  26. }
  27. }
  28.  
  29. foreach($tmp as $mail => $subj)
  30. $result[] = [ // Просто приводим к требуемому виду
  31. 'forms_email' => $mail,
  32. 'form_name' => implode(', ', $subj)
  33. ];
  34.  
  35. unset($tmp);
  36.  
  37. var_dump($result);
Success #stdin #stdout 0.03s 52432KB
stdin
Standard input is empty
stdout
array(3) {
  [0]=>
  array(2) {
    ["forms_email"]=>
    string(13) "max@gmail.com"
    ["form_name"]=>
    string(18) "form_compl, TESTED"
  }
  [1]=>
  array(2) {
    ["forms_email"]=>
    string(12) "sm@yandex.ru"
    ["form_name"]=>
    string(21) "form_compl, formname2"
  }
  [2]=>
  array(2) {
    ["forms_email"]=>
    string(10) "123@da.fwe"
    ["form_name"]=>
    string(6) "TESTED"
  }
}