Good evening everyone. I need your advice professionals. I have an array (this is data from LDAP) in which there are identical users with names (displayname), but they have different email addresses. How can I choose those who have yandex.ru in their email (in fact, AD has the same user as User and Contact. And I have to select the contact, since he has yandex.ru email address. But this is a digression ...). I think it’s about sorting or even grouping.

$entries[1]["displayname"][0]='Alex'; $entries[1]["email"][0]='123@mail.ru'; $entries[2]["displayname"][0]='Alex'; $entries2[2]["email"][0]='256@yandex.ru'; $entries[3]["displayname"][0]='Ann'; $entries[3]["email"][0]='789@mail.ru'; $entries[4]["displayname"][0]='Ann'; $entries[4]["email"][0]='555@yandex.ru'; 
  • 2
    foreach($entries as $entry){if(strpos($entry["email"][0], "@yandex.ru") !== false) echo "YES";} - rjhdby
  • cool. Now I will try to implement it, until someone gave an answer :) thank you. - Oleg Zolotarenko
  • Do you have 2 arrays? $entries and $entries2 ? - Raz Galstyan
  • no, no, - an array of one - entries, and of course instead of the first elements 1,2,3 in it $ i runs - Oleg Zolotarenko
  • 3
    I'm behind array_filter($entries, function($v){ return stristr($v['email'][0], "@yandex.ru"); }); - teran

2 answers 2

This is how you can do it, see if you do not delete the item from Yandex. Well, of course, you can also insert into another array if Yandex (so that the initial array doesn't spoil.).

 $entries[0]["displayname"][0]='Alex'; $entries[0]["email"][0]='123@mail.ru'; $entries[1]["displayname"][0]='Alex'; $entries[1]["email"][0]='256@yandex.ru'; $entries[2]["displayname"][0]='Ann'; $entries[2]["email"][0]='789@mail.ru'; $entries[3]["displayname"][0]='Ann'; $entries[3]["email"][0] = '555@yandex.ru'; echo '<pre>'; for($i=0;$i<count($entries);$i++){ if(strpos($entries[$i]["email"][0], 'yandex.ru') == false){ unset($entries[$i]); } } print_r($entries); 

    I think you can just go through the loop and build a new one:

     $userByYandex = array(); for($i=0;$i<count($entries);$i++){ $domain = explode('@',$entries[$i]["email"][0]); if($domain[1] == 'yandex.ru'){ array_push($userByYandex,$entries[$i]["email"][0]); } } print_r($userByYandex); 
    • can be and with the help strpos() - Raz Galstyan
    • Yes, of course, @rjhdby gave an example, so this one is for variety) - Arsen