Search an array for a matching string

前端 未结 7 1367
渐次进展
渐次进展 2021-01-23 01:26

I am looking for away to check if a string exists as an array value in an array is that possible and how would I do it with PHP?

7条回答
  •  轮回少年
    2021-01-23 02:00

    Say we have this array:

     'foo',
      2 => 'bar',
      3 => 'baz',
    );
    ?>
    

    If you want to check if the element 'foo' is in the array, you would do this

    
    

    If you want to get the array index of 'foo', you would do this:

    
    

    Also, a simple rule for the order of the arguments in these functions is: "needle, then haystack"; what you're looking for should be first, and what you're looking in second.

提交回复
热议问题