Pull data from multidimension array if a string value is defined - php

I want to pull the string value of [totalPrice] , [boardType] , [roomCategory] if the [hotelCode] value is known.
The array print_r is (there only one hotel,)
Array (
[0] => stdClass Object (
[processId] => H5-84752260
[hotelCode] => GRSCDS
[availabilityStatus] => InstantConfirmation
[totalPrice] => 40 [totalTax] => 0
[totalSalePrice] => 0
[currency] => EUR
[boardType] => Room and Breakfast (American Buffet Breakfast)
[rooms] => Array (
[0] => stdClass Object (
[roomCategory] => Twin Room
[paxes] => Array (
[0] => stdClass Object (
[paxType] => Adult
[age] => 30 )
[1] => stdClass Object (
[paxType] => Adult
[age] => 30 ) )
[totalRoomRate] => 40
[ratesPerNight] => Array (
[0] => stdClass Object (
[date] => 2015-03-11 [amount] => 40 ) ) ) ) ) )

You can iterate array, check item with if condition and grab values you need. Like this:
<?php
foreach ($array as $item) {
if ($item->hotelCode === 'YOUR_CODE') {
// get your data here using $item->totalPrice, etc
break;
}
}

Related

PHP parse soapClient response transfer to a table

I'm just on the hose and dont get on.
I am not a professional programmer but have so far with much reading and over numerous attempts everything so far created what I have undertaken, only with this I have probably found my masterpiece.
I have a response of a SOAP query and would like to display the values in a table for each powerUnitidentifier. What is the best way to do this?
(
[RawData] => stdClass Object
(
[from] => 2022-05-10T01:00:00+02:00
[to] => 2022-05-10T01:20:00+02:00
[dataRecords] => stdClass Object
(
[record] => Array
(
[0] => stdClass Object
(
[powerUnitIdentifier] => abc123
[time] => 2022-05-10T01:00:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.27
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[1] => stdClass Object
(
[powerUnitIdentifier] => zyx321
[time] => 2022-05-10T01:00:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.19
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[2] => stdClass Object
(
[powerUnitIdentifier] => abc123
[time] => 2022-05-10T01:10:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.15
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
[3] => stdClass Object
(
[powerUnitIdentifier] => zyx321
[time] => 2022-05-10T01:10:00+02:00
[fields] => stdClass Object
(
[field] => Array
(
[0] => stdClass Object
(
[identifier] => 100
[value] => 0
)
[1] => stdClass Object
(
[identifier] => 101
[value] => 3.09
)
[2] => stdClass Object
(
[identifier] => 102
[value] => 70.00
)
)
)
)
)
)
)
)```
You loop over the section of the data, starting the foreach loop at the right level of your data structure
foreach($theName->RawData->DataRecords->record as $obj) {
echo $obj->powerUnitIdentifier;
}
Or if you ment to process the sub array of that
foreach($theName->RawData->DataRecords->record as $obj) {
echo $obj->powerUnitIdentifier . '<br>';
foreach( $obj->fields as $field) {
echo $field->identifier . ',' . $field->value . '<br>';
}
}

Unset elements from a multidimensional array

I am trying to unset/remove some elements from a multidimeansional usign the below code but i cant figure it out how.
My array looks like (only two elements from my multidimensional array)
[3] => stdClass Object
(
[processId] => H7-99440469
[hotelCode] => TR4SWV
[availabilityStatus] => InstantConfirmation
[totalPrice] => 36
[totalTax] => 0
[totalSalePrice] => 0
[currency] => EUR
[boardType] => All Inclusive
[rooms] => Array
(
[0] => stdClass Object
(
[roomCategory] => Double Promotional
[paxes] => Array
(
[0] => stdClass Object
(
[paxType] => Adult
[age] => 30
)
[1] => stdClass Object
(
[paxType] => Adult
[age] => 30
)
)
[totalRoomRate] => 36
[ratesPerNight] => Array
(
[0] => stdClass Object
(
[date] => 2015-05-01
[amount] => 36
)
)
)
)
)
[4] => stdClass Object
(
[processId] => HH-46795719
[hotelCode] => TRIIFY
[availabilityStatus] => InstantConfirmation
[specialDeal] => 11
[totalPrice] => 38
[totalTax] => 0
[totalSalePrice] => 0
[currency] => EUR
[boardType] => All Inc.
[rooms] => Array
(
[0] => stdClass Object
(
[roomCategory] => Double Room
[paxes] => Array
(
[0] => stdClass Object
(
[paxType] => Adult
[age] => 30
)
[1] => stdClass Object
(
[paxType] => Adult
[age] => 30
)
)
[totalRoomRate] => 38
[ratesPerNight] => Array
(
[0] => stdClass Object
(
[date] => 2015-05-01
[amount] => 38
)
)
)
)
)
How can i remove all the elemets that dont have [specialDeal] => 11 ? [specialDeal] => 11 is the Early booking discount
for($i=0;$i<count($array);$i++) {
if (!(isset($array[$i]->specialDeal) && $array[$i]->specialDeal] === 11)) {
unset($array[$i]);
}
}
$otherspecialoffer = array();
foreach( $availHotels as $key=>$item ) {
if (!(isset($item->specialDeal) && $item->specialDeal === 11)) {
unset($availHotels[$key]);
} else {
$otherspecialoffer[$item->specialDeal] = $key;
}
}

Pull data from a PHP object

I am trying to pull data from a PHP object, but I can't get all the data which is needed.
Object $hotel1 is:
stdClass Object (
[processId] => HH-24896940
[hotelCode] => TRN367
[availabilityStatus] => InstantConfirmation
[totalPrice] => 398
[totalTax] => 0
[totalSalePrice] => 0
[currency] => EUR
[boardType] => Half Board
[rooms] => Array ( [0] => stdClass Object (
[roomCategory] => Double Standard [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 )
[1] => stdClass Object (
[paxType] => Adult [age] => 30 ) )
[totalRoomRate] => 398
[ratesPerNight] => Array ( [0] => stdClass Object (
[date] => 2015-01-27
[amount] => 398 ) ) ) ) )
Code used is :
<?php
foreach ($availHotels as $hotel1) {
if ($hotel1->hotelCode === $code1) {
break;
}
}
?>
And I pull data with:
$hotel1->boardType , $hotel1->totalPrice
But when I am trying to get roomCategory is not working with
$hotel1->roomCategory
"roomCategory" is nested in "room".
Here is an example how to get what you expect:
$obj = (object) array(
'boardType' => 'foo',
'room' => array(
(object) array(
'roomCategory' => 'bar'
)
)
);
print_r($obj);
echo $obj->boardType;
echo $obj->room[0]->roomCategory;
$obj would be $hotel1 in your case.

Split multidimensional array from SOAP response

I hae vthe below code
<?php
if (is_object($checkAvailability->availableHotels))
$hotelResponse[] = $checkAvailability->availableHotels;
$hotelResponse = $checkAvailability->availableHotels;
foreach ((array)$hotelResponse as $hnum => $hotel)
?>
$hotelResponse is a Multi-dimensional array
print_r($checkAvailability->availableHotels); is generating :
Array ( [0] => stdClass Object ( [processId] => H0-41925041 [hotelCode] => ITJHRV [availabilityStatus] => InstantConfirmation [totalPrice] => 1421 [totalTax] => 0 [totalSalePrice] => 1509.38 [currency] => EUR [boardType] => Breakfast Buffet [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Classic Double or Twin Room-1 queen bed [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 1421 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-04-20 [amount] => 203 ) [1] => stdClass Object ( [date] => 2015-04-21 [amount] => 203 ) [2] => stdClass Object ( [date] => 2015-04-22 [amount] => 203 ) [3] => stdClass Object ( [date] => 2015-04-23 [amount] => 203 ) [4] => stdClass Object ( [date] => 2015-04-24 [amount] => 203 ) [5] => stdClass Object ( [date] => 2015-04-25 [amount] => 203 ) [6] => stdClass Object ( [date] => 2015-04-26 [amount] => 203 ) ) ) ) ) [1] => stdClass Object ( [processId] => HA-51032431 [hotelCode] => ITRR5G [availabilityStatus] => InstantConfirmation [totalPrice] => 1590 [totalTax] => 0 [totalSalePrice] => 0 [currency] => EUR [boardType] => Bed & Breakfast [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Twin Room (Including Breakfast and Wi-Fi) [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 1590 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-04-20 [amount] => 197 ) [1] => stdClass Object ( [date] => 2015-04-21 [amount] => 197 ) [2] => stdClass Object ( [date] => 2015-04-22 [amount] => 239 ) [3] => stdClass Object ( [date] => 2015-04-23 [amount] => 239 ) [4] => stdClass Object ( [date] => 2015-04-24 [amount] => 239 ) [5] => stdClass Object ( [date] => 2015-04-25 [amount] => 239 ) [6] => stdClass Object ( [date] => 2015-04-26 [amount] => 240 ) ) ) ) ) )
How can i do that get every response by echoing them as below
<?php echo $hotel->hotelCode?>
<?php echo $hotel->totalPrice?>
As i understand i need to convert the 2d array into a object but from there i have no clue. Please help.
Try this:
$hotelCodes = array() ;
$availHotels = $checkAvailability->availableHotels ;
foreach($availHotels as $hotel){
$hotelCodes[] = $hotel->hotelCode ;
//echo $hotel->hotelCode ;
//echo $hotel->totalPrice ;
}
Now you have $hotelCodes array and access it like $hotelCodes[0], $hotelCode[1] and so more.

How to get the Multidimensional array value in foreach loop using php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
This my code ..... I need output of hotel name, id, selling price etc
array value will be loop because I have get result in bulk manner .
Array
(
[TestMode] => true
[HotelAvailability] => Array
(
[0] => Array
(
[Hotel] => Array
(
[Id] => 3027405
[Name] => Royal Plaza
[Region] => Array
(
[Id] => 16849
[Name] => Chennai (Madras)
)
[Type] => Hotel
[Stars] => 3
[Rank] => 1
)
[Result] => Array
(
[QuoteId] => 12249136-10
[Room] => Array
(
[RoomType] => Array
(
[Code] => 1002196
[Text] => Double Standard
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1824.26
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645204
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 912.13
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645205
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 912.13
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
)
[1] => Array
(
[Hotel] => Array
(
[Id] => 2118726
[Name] => Days Inn Deccan Plaza
[Region] => Array
(
[Id] => 16849
[Name] => Chennai (Madras)
)
[Type] => Hotel
[Stars] => 3
[Rank] => 1
)
[Result] => Array
(
[QuoteId] => 12249136-36
[Room] => Array
(
[RoomType] => Array
(
[Code] => 1002196
[Text] => Double Standard
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 3192.90
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645292
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1596.45
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645293
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1596.45
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
)
[2] => Array
(
[Hotel] => Array
(
[Id] => 723729
[Name] => Green Park
[Region] => Array
(
[Id] => 16849
[Name] => Chennai (Madras)
)
[Type] => Hotel
[Stars] => 4
[Rank] => 1
)
[Result] => Array
(
[0] => Array
(
[QuoteId] => 12249136-33
[Room] => Array
(
[RoomType] => Array
(
[Code] => 1004527
[Text] => Double Or Twin Deluxe
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 3273.82
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645286
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1636.91
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645287
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1636.91
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
[1] => Array
(
[QuoteId] => 12249136-34
[Room] => Array
(
[RoomType] => Array
(
[Code] => 2062742
[Text] => Double or Twin BUSINESS-CLUB
)
[MealType] => Array
(
[Code] => 1000018
[Text] => Bed and breakfast
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 3863.18
[Estimated] => false
[Converted] => false
)
[Guests] => Array
(
[Adult] => Array
(
[0] => Array
(
[Id] => -252645288
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1931.59
[Estimated] => false
[Converted] => false
)
)
[1] => Array
(
[Id] => -252645289
[Forename] => Array
(
)
[Surname] => Array
(
)
[SellingPrice] => Array
(
[Currency] => INR
[Amount] => 1931.59
[Estimated] => false
[Converted] => false
)
)
)
)
[Confirmation] => allocation
)
)
)
)
If I understood your question correctly, you would like to extract the Hotel Id, Name, and SellingPrice keys from this array, in a loop.
If we assume the above array is stored in a variable called $HotelData, and that the array structure is consistent with the above output in your question, then the following code should allow you to do what you want...
foreach($HotelData['HotelAvailability'] as $hotel) {
$id = $hotel['Hotel']['Id'];
$name = $hotel['Hotel']['Name'];
$price = $hotel['Result']['Room']['SellingPrice']['Currency'] . ' ' . $hotel['Result']['Room']['SellingPrice']['Amount'] ;
echo "$id - $name: $price<br>\n";
}
/* This should output something like
*
* 3027405 - Royal Plaza: INR 1824.26
* 2118726 - Days Inn Deccan Plaza - INR 3192.90
* ...
*/
foreach($data['HotelAvailability'] AS $record) {
echo 'Id: '; echo $record['Hotel']['Id'];
echo '<br />';
echo 'Name: '; echo $record['Hotel']['Name'];
echo '-------------'; echo '<br />';
}

Categories