0

i develop the program that get the array of user skills contain user_id and their skills, this array like this :

array(4) {
   [17]=>
     array(3) {
        [0]=>
          string(8) "word"
        [1]=>
          string(10) "power point"
        [2]=>
          string(28) "excel"
       }
   [16]=>
    array(3) {
    [0]=>
     string(14) "english
    [1]=>
     string(14) "french"
    [2]=>
    string(12) "farsi"
  }
 }

the key is user_id and the value is array of skills, now i want to search the user_ids that have two value : english Or word and i expect to return 17, 16 , how can i do that? , thanks for you help :)

Amirreza Moradi
  • 221
  • 1
  • 4
  • 19
  • i saw that question , i think that search id in array , i just search multi value , can i do that method again? – Amirreza Moradi Aug 09 '17 at 07:03
  • user id 16 & 17 don't have two value that you want (english,word) ! They have only one value in each id – Jack jdeoel Aug 09 '17 at 07:04
  • StackOverflow is not a tutorial site nor search engine replacement. We can help, but it's your job to work on this in first place. [Put some efforts](http://meta.stackoverflow.com/questions/261592) first, then ask with a clear explanation and [MCV example](http://stackoverflow.com/help/mcve) if applicable. – Milan Chheda Aug 09 '17 at 07:05
  • @ShahinA.M, change your description to *to search the user_ids that have two value : english **OR** word* – RomanPerekhrest Aug 09 '17 at 07:08
  • i know what is StackOverflow and it's purpose , i just want to know other ideas , so , you can ignore my question and enjoy :) – Amirreza Moradi Aug 09 '17 at 07:10
  • @RomanPerekhrest done it! , please share us your solution , thanks alot :) – Amirreza Moradi Aug 09 '17 at 07:12

1 Answers1

2

The solution using array_intersect function:

$arr = [ 17 => ["word", "power point", "excel"], 16 => ["english", "french", "farsi"] ];
$search_keys = ["english", "word"];
$keys = [];

foreach ($arr as $k => $v) {
    if (array_intersect($search_keys, $v)) $keys[] = $k;
}

print_r($keys);

The output:

Array
(
    [0] => 17
    [1] => 16
)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105