Rohan
Rohan

Reputation: 1657

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.

Upvotes: 0

Views: 66

Answers (2)

Young
Young

Reputation: 8356

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

Upvotes: 1

St. John Johnson
St. John Johnson

Reputation: 6660

Try this:

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

Upvotes: 2

Related Questions