Reputation: 37
I have a multidimensional array I wish to search through it in PHP.
rgInventory": {
"2294085379": {
"id": "2294085379",
"classid": "520025252",
"instanceid": "0",
"amount": "1",
"pos": 41
},
"2383675126": {
"id": "2383675126",
"classid": "310781918",
"instanceid": "0",
"amount": "1",
"pos": 40
}
//and so on...
As you can see the 2nd dimensional array is a spontaneous number. I am wishing to search for the classid, I would have the classid provided but how would I search for it, as I would want to find the id from the class id.
Upvotes: 0
Views: 55
Reputation: 78994
After $result = json_decode($json, true)
:
foreach($result['rgInventory'] as $array) {
if($array['classid'] == $classid) {
echo $array['id'];
}
}
Or a slicker way maybe:
echo array_column($result['rgInventory'], 'id', 'classid')[$classid];
Or even:
echo array_search($classid, array_column($result['rgInventory'], 'classid', 'id'));
Upvotes: 2