0

I'm looking for an alternative function that works the same as in_array but which could also check if the search term only contains a part of the given element instead of the whole element:

Currently working with the following script:

$attributes = array('dogs', 'cats', 'fish');

  if (in_array($attributes, array('dog','cats','fishess'), true )) {

    * does something for cats, but not for dogs and fish
      because the function only checks if the given term is identical to the word in the array instead of only a part of the word *
} 

How would I build my up function so that it passes words which only contain parts of word in the array aswell?

Preferred example would look something like this:

$words = array('fish', 'sharks');

if (*word or sentence part is* in_array($words, array('fishing', 'sharkskin')){

return 'your result matched 2 elements in the array $words

}
Gianni
  • 19
  • 3
  • http://php.net/manual/en/function.array-intersect.php – Matt Apr 04 '17 at 19:09
  • 1
    Better yet http://php.net/manual/en/function.array-uintersect.php – Dan Apr 04 '17 at 19:12
  • An important detail for this kind of question is what is the size of your needle array and what is the size of your haystack array. Depending of these sizes the answer can be totally different if performance matters. – Casimir et Hippolyte Apr 04 '17 at 22:46

5 Answers5

1

The solution using array_filter and preg_grep functions:

$words = ['fish', 'sharks', 'cats', 'dogs'];
$others = ['fishing', 'sharkskin'];

$matched_words = array_filter($words, function($w) use($others){
    return preg_grep("/" . $w . "/", $others);
});

print_r($matched_words);

The output:

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

Try the code below:

<?php
$what  = ['fish', 'sharks'];
$where = ['fishing', 'sharkskin'];

foreach($what as $one)
    foreach($where as $other)
        echo (strpos($other, $one)!==false ? "YEP! ".$one." is in ".$other."<br>" : $one." isn't in ".$other."<br>");
?>

Hope it helps =}

rafsb
  • 31
  • 4
  • It's the basic way without artifice (since the loops are explicit and since you use `strpos` that suffices for this task). However instead of displaying shit (and assuming the user wants html), it's better to return a filtered array. – Casimir et Hippolyte Apr 04 '17 at 22:38
  • Hi @CasimiretHippolyte, I believe he will make a better use of the results, I've displayed some shit because he can run this code (without modifying it) and see if it fits his problem... Sometimes simplicity is smarter... – rafsb Apr 05 '17 at 23:17
0

you can use:

array_filter($arr, function($v, $k) {
    // do whatever condition you want
    return in_array($v, $somearray);
}, ARRAY_FILTER_USE_BOTH);

this function call on every item in the array $arr a function that you can customize, in your case check whether you element in another array or not

0

Why not just make your own piece of code / function?

foreach ($item in $attributes) {
    foreach ($item2 in array('dog','cats','fishess')) {
        // Check your custom functionality.
        // Do something if needed.
    }
}

You can take a look at array_intersect, but it is not going to check pattern matches (which you have somehow mentioned?)

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

foreach (array_intersects($attributes, array('dog','cats','fishess') {
    // do something.
}
Ajk_P
  • 1,874
  • 18
  • 23
0

I would go with:

 $patterns = array('/.*fish.*/', '/.*sharks.*/'); 
 $subjects = array('fishing', 'aaaaa', 'sharkskin');
 $matches = array();
 preg_replace_callback(
    $patterns,
    function ($m) {
        global $matches;
        $matches[] =  $m[0];
        return $m[0];
    },
    $subjects
 );

 print_r($matches); // Array ( [0] => fishing [1] => sharkskin )
bluehipy
  • 2,254
  • 1
  • 13
  • 19
  • There are nothing to replace, don't use `preg_replace...` and don't use `global` at all whatever the reason. – Casimir et Hippolyte Apr 04 '17 at 22:30
  • @CasimiretHippolyte why not use global? It is a language feature ;) It is obvious that the preg replace callback it is used for a different reason. – bluehipy Apr 04 '17 at 22:57