开发者

Searching Multidimensional Array for Value PHP

开发者 https://www.devze.com 2023-01-28 04:52 出处:网络
I feel like this is a pretty easy question, but I can\'t seem to find the answer anywhere. $array = (\'colors\' => array(\'red\',\'orange\'),

I feel like this is a pretty easy question, but I can't seem to find the answer anywhere.

$array = ('colors' => array('red','orange'),
          'numbers'=> array('one','two')
         );

How do a perform a search on the $array to determine if colors contai开发者_如何学Cns a value of red?

Thanks in advance.


There is no built in array function to do this. Thes simplest way to to it in the specific case youve given is to do:

$hasRed = (isset($array['colors'] && in_array('red', $array['colors']));


Simplest way would be;

$hasRed = isset($array['colors']['red']);


Try this:

array_search("red", $array[ "colors" ])
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号