Jump to content

Recommended Posts

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. 
 

Screen Shot 2022-11-05 at 9.22.52 PM.png

image.png

Link to comment
https://forums.phpfreaks.com/topic/315495-get-array-from-post-api-in-foreach-loop/
Share on other sites

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.