Lalarukh Posted November 5, 2022 Share Posted November 5, 2022 I'm calling an array from Postman API in which I have array like this in first image. And getting its values inside my through this PHP code: foreach ($request->room_prices as $roomCode => $roomPrices) { $value .= $this->helper($roomCode, $roomPrices); // this is how its storing the prices of an id i.e. 569694 } And now I've multiple arrays inside the api endpoint and I can't figure out a way to get the id and day values like (avail) & (no_ota) | Second image is the API request. As in the rooms_availability array we have 2 objects: 1-ID and 2-Days. And days also have multiple array like avail and no_ota. I can't figure out the way I can call these avail, no_ota and id of room inside foreach loop. foreach ($request->room_availabilities as $roomCodes => $roomCode) { foreach ($roomCode => array_combine($avail, $no_ota)) { $value .= $this->helper($roomCode, $avail, $no_ota); } } Seeking experts guidance to get id, avail and no_ota through foreach loop into my function. Quote Link to comment https://forums.phpfreaks.com/topic/315495-get-array-from-post-api-in-foreach-loop/ Share on other sites More sharing options...
Barand Posted November 5, 2022 Share Posted November 5, 2022 Thank you for the pretty pictures. If you had posted a json string that could be copy/pasted intp my editor I might have been able to process it and show you how. Quote Link to comment https://forums.phpfreaks.com/topic/315495-get-array-from-post-api-in-foreach-loop/#findComment-1602227 Share on other sites More sharing options...
requinix Posted November 5, 2022 Share Posted November 5, 2022 6 hours ago, Lalarukh said: I can't figure out the way I can call these avail, no_ota and id of room inside foreach loop. foreach ($request->room_availabilities as $roomCodes => $roomCode) { foreach ($roomCode => array_combine($avail, $no_ota)) { $value .= $this->helper($roomCode, $avail, $no_ota); } } If you don't understand the nature of the problem then throwing random code at it will not help. Thus: 6 hours ago, Lalarukh said: Seeking experts guidance to get id, avail and no_ota through foreach loop into my function. My expert guidance is that you need to learn about (1) arrays and objects in JSON, (2) arrays and objects in PHP, and (3) how you mentally and programmatically translate from one to the other. Here's how you read the JSON: { "rooms_availabilities": [ // do a foreach on $request->rooms_availabilities to get { // ...a set of objects. each object has "id": 570386, // ...a ->id "days": [ // ...and a ->days, which is another array. foreach on it to get { // ...a set of objects. each object has "avail": 5, // ...a ->avail "no_ota": 1 // ...and a ->no_ota Quote Link to comment https://forums.phpfreaks.com/topic/315495-get-array-from-post-api-in-foreach-loop/#findComment-1602234 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.