在foreach中使用in_array并取出数组具有匹配值

I am having issues with using in_array() inside a foreach loop. I have an array like this

[0] =>
    name=>1
    type=>foo
[1] =>
    name=>2
    type=>bar
[2] =>
    name=>3
    type=>bar
[3] =>
    name=>4
    type=>foo

I'm using

if(in_array('foo',$array->type){ *my echo code*}

to take out all array that have type = foo but it also take out unmatch array. Where am I wrong?

Maybe you are doing it incorrectly..

foreach($yourarr as $k=>$arr)
{
 if(in_array('foo',$arr))
 {
   echo key($arr); //"prints" name , name
 }
}

Working Demo

in your case $array->type is not an array. it is a string. you should compare it like string comparison.

if('foo' === $array['type']){ *my echo code*}

you also change array access.

You need array syntax and comparison:

if('foo' == $array['type']) {
  /* your code */
}