-4

I have arrays in my JSON like this:

[
    {
        "id":"bobbys_burgers",
        "is_enabled":true,
        "name":"Bobbys Burgers",
        "supported_transactions":[
            "222",
            "111",
            "333"
        ],
        "enrollment_required":[
        ],
        "restricted_transactions":[
            "123"
        ]
    },

    {
        "id":"randys_sandwich",
        "is_enabled":true,
        "name":"Randys Sandwich",
        "supported_transactions":[
            "321"
        ],
        "enrollment_required":[
        ],
        "restricted_transactions":[
        ]
    },
]

I want to get all the keys of the whole array where id = randys_sandwich. for example, i want to search for id == randys_sandwich and return the is_enabled, name, supported_transactions, etc AND their values from that array. how can i do that in php?

Jason Bale
  • 363
  • 2
  • 7
  • 14
  • I got my answer here: http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php – Jason Bale May 12 '17 at 22:24

1 Answers1

-1

You need to json_decode your array then loop through the objects in your array find the one with the id you're looking for.

$array = json_decode($json, true);

foreach ($item in $array) {
  if ($item['id'] == 'randys_sandwich') {
    var_dump($item);
    break; // Once you've found the item no need to continue the loop
  }
}
Ryan Tuosto
  • 1,941
  • 15
  • 23