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;
}
}
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
i have data in json something like that:
stdClass Object
(
[contacts] => stdClass Object
(
[14] => stdClass Object
(
[data] => stdClass Object
(
[email] => veer#gmail.com
[first_name] => veer
[last_name] =>
[user_id] => 16
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 14
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => veer
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 92
[1] => 13
[2] => 12
[3] => 9
)
[files] => Array
(
)
)
[9] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.pratibha1432#gmail.com
[first_name] => Pratibha
[last_name] => Singh
[user_id] => 8
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 9
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => pratibha
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 93
[2] => 92
[3] => 91
[4] => 82
[5] => 15
[6] => 14
[7] => 13
[8] => 9
)
[files] => Array
(
)
)
[4] => stdClass Object
(
[data] => stdClass Object
(
[email] => singh.dev1432#gmail.com
[first_name] => Devesh
[last_name] => Singh
[user_id] => 7
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 4
[gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => devesh
[profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 93
[1] => 92
[2] => 15
[3] => 12
[4] => 11
[5] => 9
)
[files] => Array
(
)
)
[2] => stdClass Object
(
[data] => stdClass Object
(
[email] => admin#gmail.com
[first_name] => veronica
[last_name] =>
[user_id] => 1
[owner_id] => 0
[optin_status] => 0
[date_created] => 2020-01-20 13:14:54
[ID] => 2
[gravatar] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[age] =>
)
[meta] => stdClass Object
(
[preferences_changed] => 1579526094
[user_login] => admin
[profile_picture] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
[birthday] =>
)
[tags] => Array
(
[0] => 94
[1] => 92
[2] => 15
[3] => 14
[4] => 13
[5] => 9
)
[files] => Array
(
)
)
)
[status] => success
)
and now I am trying to fetch "email" from the contacts->id->data->email so for that i am using this code. I am trying to loop the things by which i can fetch email from all the ids present. and in the ids section, there is data and inside data the email is present so how can i fetch all emails from all the ids.
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails = array('email'=>$value->email);
return $emails;
}
}
but it return only single data:
Array
(
[email] => veer#gmail.com
)
i want to fetch all the email like that:
Array
(
[0] => Array
(
[emails] => veer#gmail.com
)
[1] => Array
(
[emails] => singh.pratibha1432#gmail.com
)
[2] => Array
(
[emails] => singh.dev1432#gmail.com
)
[3] => Array
(
[emails] => admin#gmail.com
)
)
how can I achieve this?
Define null array and in foreach run store every email in that variable.
$emails = array();
foreach ((Array)$body->contacts as $id => $values) {
foreach ($values as $data => $value) {
$emails[] = array('emails'=>$contact->data->email;);
}
}
return $emails;
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;
}
}
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.
How can i echo each hotelCode, totalPrice, boardType and roomCategory from the below multidimmensional array
<?php
$hotelCodes = array() ;
$availHotels = $checkAvailability->availableHotels ;
foreach($availHotels as $hotel){
if (!isset($hotelCodes[$hotel->hotelCode])) {
$hotelCodes[$hotel->hotelCode] = array();
}
// you could ofcourse just take specific info from $hotel and put it in here.
$hotelCodes[$hotel->hotelCode][] = $hotel;
}
?>
and
print_r of $hotelCodes is
Array ( [HOTELCODE1] => Array ( [0] => stdClass Object ( [processId] => HV-82768336 [hotelCode] => ITULJP [availabilityStatus] => InstantConfirmation [totalPrice] => 279 [totalTax] => 0 [totalSalePrice] => 298.86 [currency] => EUR [boardType] => Breakfast Buffet [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Twin Room - Non-Refundable-1 double bed [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 279 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-03-11 [amount] => 34.84 ) [1] => stdClass Object ( [date] => 2015-03-12 [amount] => 34.88 ) [2] => stdClass Object ( [date] => 2015-03-13 [amount] => 34.88 ) [3] => stdClass Object ( [date] => 2015-03-14 [amount] => 34.88 ) [4] => stdClass Object ( [date] => 2015-03-15 [amount] => 34.88 ) [5] => stdClass Object ( [date] => 2015-03-16 [amount] => 34.88 ) [6] => stdClass Object ( [date] => 2015-03-17 [amount] => 34.88 ) [7] => stdClass Object ( [date] => 2015-03-18 [amount] => 34.88 ) ) ) ) ) [1] => stdClass Object ( [processId] => HN-10616086 [hotelCode] => ITULJP [availabilityStatus] => InstantConfirmation [totalPrice] => 294 [totalTax] => 0 [totalSalePrice] => 314.59 [currency] => EUR [boardType] => Breakfast Buffet [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Double or Twin Room-1 double bed [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 294 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-03-11 [amount] => 36.75 ) [1] => stdClass Object ( [date] => 2015-03-12 [amount] => 36.75 ) [2] => stdClass Object ( [date] => 2015-03-13 [amount] => 36.75 ) [3] => stdClass Object ( [date] => 2015-03-14 [amount] => 36.75 ) [4] => stdClass Object ( [date] => 2015-03-15 [amount] => 36.75 ) [5] => stdClass Object ( [date] => 2015-03-16 [amount] => 36.75 ) [6] => stdClass Object ( [date] => 2015-03-17 [amount] => 36.75 ) [7] => stdClass Object ( [date] => 2015-03-18 [amount] => 36.75 ) ) ) ) ) ) [HOTELCODE2] => Array ( [0] => stdClass Object ( [processId] => HZ-21733446 [hotelCode] => ITOI6P [availabilityStatus] => InstantConfirmation [totalPrice] => 295 [totalTax] => 0 [totalSalePrice] => 0 [currency] => EUR [boardType] => Room Only [rooms] => Array ( [0] => stdClass Object ( [roomCategory] => Double Or Twin Standard [paxes] => Array ( [0] => stdClass Object ( [paxType] => Adult [age] => 30 ) [1] => stdClass Object ( [paxType] => Adult [age] => 30 ) ) [totalRoomRate] => 295 [ratesPerNight] => Array ( [0] => stdClass Object ( [date] => 2015-03-11 [amount] => 37 ) [1] => stdClass Object ( [date] => 2015-03-12 [amount] => 37 ) [2] => stdClass Object ( [date] => 2015-03-13 [amount] => 37 ) [3] => stdClass Object ( [date] => 2015-03-14 [amount] => 37 ) [4] => stdClass Object ( [date] => 2015-03-15 [amount] => 37 ) [5] => stdClass Object ( [date] => 2015-03-16 [amount] => 37 ) [6] => stdClass Object ( [date] => 2015-03-17 [amount] => 37 ) [7] => stdClass Object ( [date] => 2015-03-18 [amount] => 36 ) ) ) ) ) ) )
I need to echo the values
HOTELCODE1 and HOTELCODE1 as $hotelcode[1] and $hotelcode[2]
and totalRoomRate, roomCategory and boardType for each hotelcode
Thanks
You're already there, you just need to make sure only one hotel is stored for any hotel-code.
foreach($availHotels as $hotel){
// if we do not have a hotel with this code yet
if (!isset($hotelCodes[$hotel->hotelCode])) {
// store this one there
$hotelCodes[$hotel->hotelCode] = $hotel;
}
}
// run over the available hotels
foreach( $hotelCodes as $hotel ) {
echo $hotel['hotelCode'] . ' -> ' . $hotel['totalPrice' ] . ' -> etc ';
}
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.