Pull data from a PHP object - php

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.

Related

PHP Combine Two Arrays Into One With Common ID as Text

I have two arrays that I want to combine:
Here is the input of my two arrays (notice Factor1 and Factor2 are different, but YearMonth is the same in both arrays):
Array
(
[0] => stdClass Object
(
[YearMonth] => 2022-01
[Factor1] => 627
)
[1] => stdClass Object
(
[YearMonth] => 2021-12
[Factor1] => 3006
)
[2] => stdClass Object
(
[YearMonth] => 2021-11
[Factor1] => 2456
)
[3] => stdClass Object
(
[YearMonth] => 2021-10
[Factor1] => 1482
)
[4] => stdClass Object
(
[YearMonth] => 2021-09
[Factor1] => 1266
)
[5] => stdClass Object
(
[YearMonth] => 2021-08
[Factor1] => 1981
)
)
Array
(
[0] => stdClass Object
(
[YearMonth] => 2022-01
[Factor2] => 380
)
[1] => stdClass Object
(
[YearMonth] => 2021-12
[Factor2] => 1773
)
[2] => stdClass Object
(
[YearMonth] => 2021-11
[Factor2] => 769
)
[3] => stdClass Object
(
[YearMonth] => 2021-10
[Factor2] => 700
)
[4] => stdClass Object
(
[YearMonth] => 2021-09
[Factor2] => 608
)
[5] => stdClass Object
(
[YearMonth] => 2021-08
[Factor2] => 859
)
)
Here is what I would like the output to be:
Array
(
[0] => stdClass Object
(
[YearMonth] => 2022-01
[Factor1] => 627
[Factor2] => 380
)
[1] => stdClass Object
(
[YearMonth] => 2021-12
[Factor1] => 3006
[Factor2] => 1773
)
[2] => stdClass Object
(
[YearMonth] => 2021-11
[Factor1] => 2456
[Factor2] => 769
)
[3] => stdClass Object
(
[YearMonth] => 2021-10
[Factor1] => 1482
[Factor2] => 700
)
[4] => stdClass Object
(
[YearMonth] => 2021-09
[Factor1] => 1266
[Factor2] => 608
)
[5] => stdClass Object
(
[YearMonth] => 2021-08
[Factor1] => 1981
[Factor2] => 859
)
)
I've tried array_merge() but it just appends the second array to the end of the first.
And when I try:
$array3 = [];
foreach (array_merge($array1, $array2) as $row) {
$array3[$row['YearMonth']] = ($array3[$row['YearMonth']] ?? []) + $row;
}
I receive this error:
<b>Fatal error</b>: Uncaught Error: Cannot use object of type stdClass as array in /var/www/html/wp-content/plugins/ad-inserter-pro/class.php(606) : eval()'d code:23
Any help you can provide is appreciated. Thank you.
Try something like this
// loop thru 1st array using & to treat value as a reference, so that we can modify values in that array
foreach ($array1 as &$value) {
// loop through 2nd array
foreach ($array2 as $value2) {
// look for matching YearMonth (use -> because they are objects)
if ($value->YearMonth === $value2->YearMonth) {
// when a match is found, copy the Factor2 value
$value->Factor2 = $value2->Factor2
// for efficiency exit the 2nd loop, because there is no point checking any more rows if we have already found the match
break;
}
}
// after exiting the 2nd loop, code will carry on here, and keep checking values in the 1st loop
}

Modify object serialize value

I want to change the below object serialize values for 'secret_key' and 'public_key' and then i have to unserialize again.
What is the best way to modify (without string replace option)?
__PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => RulesReactionRule
[*parent] =>
[id] => 51
[*elementId] => 1
[weight] => 0
[settings] => Array
(
)
[name] => commerce_payment_commerce_stripe
[label] => Stripe
[plugin] => reaction rule
[active] => 1
[status] => 3
[dirty] =>
[module] => commerce_payment
[owner] => rules
[access_exposed] => 0
[data] => __PHP_Incomplete_Class Object
*RECURSION*
[tags] => Array
(
[0] => Commerce Payment
)
[*children] => Array
(
[0] => __PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => RulesAction
[*parent] => __PHP_Incomplete_Class Object
*RECURSION*
[id] =>
[*elementId] => 3
[weight] => 0
[settings] => Array
(
[commerce_order:select] => commerce-order
[payment_method] => Array
(
[method_id] => commerce_stripe
[settings] => Array
(
[stripe_currency] => USD
[secret_key] => sk_test_CCVVVVCC
[public_key] => pk_test_SDFFEFEF
[display_title] => Stripe
[cardonfile] => 1
[txn_type] => authorize
)
)
)
[*elementName] => commerce_payment_enable_commerce_stripe
)
)
[*info] => Array
(
)
[*conditions] => __PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => RulesAnd
[*parent] => __PHP_Incomplete_Class Object
*RECURSION*
[id] =>
[*elementId] => 2
[weight] => 0
[settings] => Array
(
)
[*children] => Array
(
)
[*info] => Array
(
)
[*negate] =>
)
[*events] => Array
(
[0] => commerce_payment_methods
)
[*eventSettings] => Array
(
)
)

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 multidimension array if a string value is defined

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;
}
}

How to loop through a SimpleXMLElement object in Php?

How to loop through a SimpleXMLElement object?
This is what I have:
I tried in many ways to iterate, but no success:
SimpleXMLElement Object
(
[products] => SimpleXMLElement Object
(
[product] => Array
(
[0] => SimpleXMLElement Object
(
[product_name] => PROD1
[price] => 100.2
[has_special_price] => true
[special_price_desc] => DESC SPECIAL PRICE
[id_product_remote] => CUSTOMID-01
[decimals] => 0
[category_key] => 1
[packaging_key] => 2
[stock_type_key] => 1
)
[1] => SimpleXMLElement Object
(
[product_name] => PROD2
[price] => 200.2
[has_special_price] => false
[special_price_desc] => DEsdSC SPECIAL PRICE
[id_product_remote] => CUSTOMID-02
[decimals] => 0
[category_key] => 1
[packaging_key] => 2
[stock_type_key] => 1
)
)
)
[products_client] => SimpleXMLElement Object
(
[product_client] => Array
(
[0] => SimpleXMLElement Object
(
[id_client_remote] => CUSTOMCLIENT-01
[id_product_remote] => CUSTOMID-01
[custom_price] => 0
)
[1] => SimpleXMLElement Object
(
[id_client_remote] => CUSTOMCLIENT-02
[id_product_remote] => CUSTOMID-02
[custom_price] => 1
)
)
)
)
What have you tried?
foreach($xml_object->products->product as $key => $product){
echo $product->product_name;
}
Try doing:
foreach($yourVar->products->product as $key => $val) {
echo $val->product_name; /// and so on
}

Categories