I have a list. Everything should be displayed, BUT until they are found with the same name, such as Anna. And then Anna should remain, whose address is * mail.ru. But this is only if there is more than one with the same name. For example, Ivan should stay as he is alone. How to more concisely implement? The list is actually over 500 people.

$entries[0]["displayname"]="Ivan" $entries[0]["mail"]="123@mail" $entries[1]["displayname"]="Anna" $entries[1]["mail"]="aaa@yandex.ru" $entries[2]["displayname"]="Bob" $entries[2]["mail"]="bbb@yandex.ru" $entries[3]["displayname"]="Anna" $entries[3]["mail"]="123@mail.ru" 
  • in powershell, I used grouping, first all with the same name, and from the groups we remove such ones then ... provided that count> 1. And then confused .. - Oleg Zolotarenko
  • Should the last email or mail.ru remain? - teran
  • -the entire email as it is, must remain and displayname must remain (the entire array). Otherwise, the entire array of the second Anna (with the mail at the end of mail.ru) should be deleted via unset, that is, all entries [3] are not needed together by name and mail - Oleg Zolotarenko

2 answers 2

 $entries[0]["displayname"]="Ivan"; $entries[0]["mail"]="123@mail"; $entries[1]["displayname"]="Anna"; $entries[1]["mail"]="aaa@yandex.ru"; $entries[2]["displayname"]="Bob"; $entries[2]["mail"]="bbb@yandex.ru"; $entries[3]["displayname"]="Anna"; $entries[3]["mail"]="123@mail.ru"; // --------------------------------- // $newArr = []; for ($i = count($entries); $i > 0; --$i) { $user = $entries[$i - 1]; $name = $user['displayname']; $mail = $user['mail']; if (isset($newArr[$name])) { if (strpos('@mail.ru', $mail) !== false) { $newArr[$name]['mail'] = $mail; } } else { $newArr[$name] = ['mail' => $mail]; } unset($entries[$i - 1]); } print_r($newArr); // output: Array ( [Anna] => Array ( [mail] => 123@mail.ru ) [Bob] => Array ( [mail] => bbb@yandex.ru ) [Ivan] => Array ( [mail] => 123@mail ) ) 

However, the fact that there may be several people with the same name and mail.ru mail is not taken into account (for example, both Anna have mail on mail.ru)

  • ****** is working ****** - Oleg Zolotarenko
  • @OlegZolotarenko I by the way forgot to attribute unset at the end of the cycle to add the first array gradually and not to keep in memory - Alexey Shimansky

In the previous answer, the structure of the resulting array changes, so I leave my option:

 <?php $entries[0]["displayname"]="Ivan"; $entries[0]["mail"]="123@mail"; $entries[1]["displayname"]="Anna"; $entries[1]["mail"]="aaa@yandex.ru"; $entries[2]["displayname"]="Bob"; $entries[2]["mail"]="bbb@yandex.ru"; $entries[3]["displayname"]="Anna"; $entries[3]["mail"]="123@mail.ru"; $temp = []; foreach ($entries as $item) { $temp[] = $item['displayname']; } $val = array_count_values($temp); for ($i = 0, $j = count($entries, COUNT_RECURSIVE); $i < $j; $i++) { $int = isset($entries[$i]) ? $val[$entries[$i]['displayname']] : 0; if ($int > 1 && strpos($entries[$i]['mail'], 'mail.ru') === false) { unset($entries[$i]); } } echo '<pre>'; print_r($entries); echo '</pre>'; 

Result:

 Array ( [0] => Array ( [displayname] => Ivan [mail] => 123@mail ) [2] => Array ( [displayname] => Bob [mail] => bbb@yandex.ru ) [3] => Array ( [displayname] => Anna [mail] => 123@mail.ru ) ) 
  • Thank you too much. but the second answer will fail. - Oleg Zolotarenko
  • @Oleg Zolotarenko is not so important :) - Edward