Hello. There is something like this array:

$abc = array( 0 => array('user_id' => '2'), 10 => array('user_id' => '2', 'useru_id' => '4'), 11 => array('user_id' => '2', 'useru_id' => '3'), 14 => array('user_id' => '4', 'useru_id' => '2'), 16 => array('user_id' => '4', 'useru_id' => '3'), ); 

You need to convert it with the condition that user_id = 2 and useru_id = 4 or user_id = 4 and useru_id = 2 that would turn out like this:

 Array ( [10] => {Любой текст} [14] => {Любой текст} ) 

The code should be executed as quickly as possible, because The initial array can be VERY BIG.

Closed due to the fact that the question is too common for the participants user194374, aleksandr barakin , Streletz , Vartlok , lexxl 10 Aug '16 at 7:25 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

     $abc = array( 0 => array('user_id' => '2'), 10 => array('user_id' => '2', 'useru_id' => '4'), 11 => array('user_id' => '2', 'useru_id' => '3'), 14 => array('user_id' => '4', 'useru_id' => '2'), 16 => array('user_id' => '4', 'useru_id' => '3'), ); $cba = []; foreach($abc as $key => $item){ // Проверка существования обоих ключей, а так же чтобы один из них был 2, второй - 4 (порядок неважен) if((isset($item['user_id']) && isset($item['useru_id'])) && (($item['user_id'] == 4 || $item['user_id'] == 2) && ($item['useru_id'] == 4 || $item['useru_id'] == 2))) $cba[$key] = $item; // Добавляется сам массив, но можно добавить {Любой текст} } var_dump($cba); 

    https://repl.it/Ck4G/0


    Faster will not work out native constructs.

    • if to throw out, what raboatt will not be? - Jean-Claude
    • one
      In fact, here it is better to use the function of array map ... And we can stop filling the rating by answering a question for freelance obviously and not for a stack flow. - Naumov
    • @ Jean-Claude, it will be in principle ... - user207618
    • @Naumov, which is faster - a question requiring tests. It seems to me that array_map will be more expensive than a loop. Although I do not know for sure. And for freelancing somehow a small task, you got excited, probably. - user207618
    • 3
      @Naumov you do not care about the person's rating, offer your answer - Jean-Claude