romanzipp
romanzipp

Reputation: 1798

Get Key of "upper level" in array

I want to get the key of the array where, for example, "type" equals "UniqueType1" (in this case 0) in PHP. The complete array is huge and from an API, so i can't modify the raw data.

The description of my problem is pretty bad but I've never done something similar. Sorry for that.

Array
(
    [summary] => Array
        (
            [0] => Array
                (
                    [type] => UniqueType1
                    [aggregated] => Array
                        (
                             ....  
                        )

                    [modifydate] => 1389890963000
                )

            [1] => Array
                (
                    [type] => UniqueType2
                    [aggregated] => Array
                        (
                             ....  
                        )

                    [modifydate] => 1389890963000
                )
  )       )

Upvotes: 0

Views: 111

Answers (1)

Patrick Q
Patrick Q

Reputation: 6393

Unless I'm missing something, this looks like a case of simply iterating through an array and checking the value of a specific key in a sub-array.

Assuming that $array is your outer array...

foreach($array["summary"] as $index => $row)
{
    if($row["type"] == "UniqueType1")
    {
        $targetIndex = $index;
        break;
    }
}

echo "The target index is " . (isset($targetIndex) ? $targetIndex : "not found.");

Upvotes: 1

Related Questions