Matching multiple values ​​with an array

My problem:

I have an array called $ ownerArray that needs to be checked by another array, and if there is a key in both arrays that displays the value of the corresponding key. $ ownerArray is populated with a database, so I can't just write the ir operator in an if statement.

$ ownerArray will look like this:

$ownerArray = array(0 =>'Name0',1 =>'Name1',2 =>'Name2',3 =>'Name3');

Then I have another array called $ Users, which has a different number of values ​​depending on what the user selects, so $ Users might look like this:

$Users = '1,2'

or like this:

$Users = '1,3'

$ Users are never the same.

But I need $ value $ ownerArray to display if any of the $ Users integer values ​​matches any $ key from $ ownerArray

Example:

foreach($ownerArray as $key => $value) 
            { 
                if(in_array($key,array($Users)))
                {
                    print $value; 
                } 
            }

. , .

, $Users = '1,3' for Name1 Name3 $ownerArray.

!

ps , if ($ key == 1 || $key == 2), .

+3
3
$merged = array_flip(array_intersect(array_flip($owners), explode(',', $users)));
+4

-

<?php

$ownerArray = array(0 =>'Name0',1 =>'Name1',2 =>'Name2',3 =>'Name3');
$users = explode(',','1,2');

if(count($users) > 0){
    foreach($users as $user){
        if($key = array_search($user,$ownerArray)){
            echo $key;
        }
    }
}


?>
+1

Just invert your logic. You actually want to cycle through your users and print something if they exist in the owner array, and not vice versa. (Sorry if this code is a bit off, but you get this idea.)

foreach($Users as $value)
{
  if(in_array($value,array($ownerArray)))
  {
    print $ownerArray[$value]; 
  }
} 
0
source

All Articles