-1

I have a given array:

$array = array('one','two','three_num3','four');

and a dynamic variable:

$term = $_GET['term'];

I'm currently using array_search() to search $array:

$target = array_search($term,$array);
if ($target > -1) {
  echo $target;
} else {
  echo 'No Target Result';
}

$target yields no result if $term is just 'three' or 'num3'.

I want to be able to find 'three_num3' in the $array where $term is either 'three' or 'num3' and is a match for 'three_num3'.
In other words, I want the underscore to act as a sort of divider, but effectively remain as one string.

Any simple way to go about this?

  • Iterate over array, check with `==` or `strpos`. – u_mulder Aug 03 '22 at 11:10
  • 1
    Does this answer your question? [Filter multidimensional array based on partial match of search value](https://stackoverflow.com/questions/6932438/filter-multidimensional-array-based-on-partial-match-of-search-value) – user3783243 Aug 03 '22 at 12:10

2 Answers2

0

You can use this loop for this

your code is

$array = array('one','two','three_num3','four');
$term = $_GET['term'];

if(in_array($term,$array)){
    echo $term;
}

foreach($array as $key=>$value){
    $ex_val=explode('_',$value);
    if(count($ex_val) == 2){
           if($term == explode('_',$value)[0]){
       echo $array[$key];
    }elseif($term ==explode('_',$value)[1]){
        echo $array[$key];
    }
    }

}
randomUser
  • 37
  • 1
  • 3
-1

You can use array_filter with custom function :) array_search checks full value in array props