0

Hi I have the following code structure

$gender = array('girl','boy','men','women');
$content = array('Fastrack men watch');

I want to search gender in content and return particular gender of that as shown below

$result='men'

it shuld search in content array if any of gender matches it should return that gender name

Thank you in advance

Siraj M
  • 21
  • 8
  • 2
    just use `foreach` and `strpos` with an `if`, what's stopping you from doin it? – Kevin Sep 28 '16 at 04:15
  • strpos returns count but i want the word which matches . @Ghost – Siraj M Sep 28 '16 at 04:22
  • @AmiteshKumar $gender array contains many different strings where as $content array contains only one string – Siraj M Sep 28 '16 at 04:23
  • @AmiteshKumar how could that be a duplicate? array intersect does not check substrings, where's the answer in that dup question that answers this question? – Kevin Sep 28 '16 at 04:23
  • 1
    you can refer following answer - http://stackoverflow.com/questions/12315536/search-for-php-array-element-containing-string#answer-12315645 – Developer Sep 28 '16 at 04:26
  • Thank you I found the solution for that . I have posted the answer. Thank you @Amitesh Kumar – Siraj M Sep 28 '16 at 04:31

4 Answers4

1
$gender = array('girl','boy','men','women');
$content = array('Fastrack men watch');
$ret=array();
foreach($gender as $val){
$pos = strpos($content[0], $val);
    if($pos!=false){
    $ret[]=$val;    
    }
}
echo "The gender found ".implode(',',$ret);
Pradyut Manna
  • 588
  • 1
  • 3
  • 12
  • Thank you This also works perfect but I used differen method as I have posted the answer for that – Siraj M Sep 28 '16 at 04:34
0
$gender = array('girl','boy','men','women');
$content = array('Fastrack men watch');
$data= explode(" ",$content[0]);

$result = array_intersect($gender, $data);

This way I found the exact result Thank you for guidance

Siraj M
  • 21
  • 8
0
        $gender_array = array('girl','boy','men','women');
        $content = 'Fastrack women watch girl';
        $content_array = explode(" ",$content);

        $result ="";
        foreach ($gender_array as $gender) {
           if(in_array($gender, $content_array)){
           $result = $result.",".$gender;
           }
        }
       echo $result = trim($result, ','); 

This shows gender gender for one or many too.. Eg: $content = 'Fastrack women girl boy watch'; Result as girl,boy,women

Janaka
  • 398
  • 5
  • 16
0

You may use array_intersect() for performing the operation that you need.

array_intersect() - Computes the intersection of arrays

array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

The array_intersect() function compares the values of two (or more) arrays, and returns the matches.

This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.

Return Value: Returns an array containing the entries from array1 that are present in all of the other arrays.

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_intersect($a1,$a2);
print_r($result);
?>

Output:

Array ( [a] => red [b] => green [c] => blue )

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

Naresh Kumar P
  • 4,127
  • 2
  • 16
  • 33