开发者

PHP arrays & keys - fetching particular ones

Lets say I have an array with a structure like this:

$arr= Array(
    array(
    "id"=>"a"
    "type">"apple"),

开发者_如何转开发    array(
    "id"=>"b"),

    array(
    "id"=>"c"),

    array(
    "id"=>"c"
    "type"=>"banana")
);

now I want to have a foreach loop which fetches all the array elements which have a key in them named "type".

Something like

foreach(all arrays which have type in them as $item)

How would I do that?

many thanks.


Try this:

 foreach ($arr as $key => $value)
   if (array_key_exists("type", $value))
     var_dump($value);


foreach($arr as $arrsub)
{
    if(isset($arrsub['type']))
    {
       //here do your stuff
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜