How to create a StdClass with multiple similar elements

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to create a StdClass with multiple similar elements



I have this code generating a request passed to a SoapClient(). This is just a portion of a much larger request object.


//OPTIONAL DAMAGE AND TRAVEL INSURANCE HANDLING
if ( $fee_insurance != 0 || $fee_damage != 0 )

$tmp_node->UnitStays->UnitStay->UnitRates->UnitRate->Rates->Rate->AdditionalCharges = new StdClass();

//TRAVEL INSURANCE
if ($fee_insurance != 0)
$AdditionalChargeINS = array(
'Quantity' => '1',
'ChargeTemplateID' => $ChargeTemplateID_INS,
'Amount' => array(
'AmountBeforeTax' => $fee_insurance,
'Taxes' => array(
'Amount' => '0.00'
)
)
);


//DAMAGE INSURANCE
if ($fee_damage != 0)
$AdditionalChargeDAM = array(
'Quantity' => '1',
'ChargeTemplateID' => $ChargeTemplateID_DAM,
'Amount' => array(
'AmountBeforeTax' => $fee_damage,
'Taxes' => array(
'Amount' => '0.00'
)
)
);


$AdditionalChargeArray = (object) array($AdditionalChargeINS,$AdditionalChargeDAM);

$tmp_node->UnitStays->UnitStay->UnitRates->UnitRate->Rates->Rate->AdditionalCharges->AdditionalCharge = $AdditionalChargeArray;



And this generates this in the request.


[AdditionalCharges] => stdClass Object
(
[AdditionalCharge] => stdClass Object
(
[0] => Array
(
[Quantity] => 1
[ChargeTemplateID] => 6595
[Amount] => Array
(
[AmountBeforeTax] => 43.62
[Taxes] => Array
(
[Amount] => 0.00
)
)
)
[1] => Array
(
[Quantity] => 1
[ChargeTemplateID] => 65802
[Amount] => Array
(
[AmountBeforeTax] => 49.00
[Taxes] => Array
(
[Amount] => 0.00
)
)
)
)
)



But I need it to end up like this (note the two nodes both called "AdditionalCharge":


[AdditionalCharges] => stdClass Object
(
[AdditionalCharge] => stdClass Object
(
[Quantity] => 1
[ChargeTemplateID] => 6595
[Amount] => Array
(
[AmountBeforeTax] => 43.62
[Taxes] => Array
(
[Amount] => 0.00
)

)
)
[AdditionalCharge] => stdClass Object
(
[Quantity] => 1
[ChargeTemplateID] => 65802
[Amount] => Array
(
[AmountBeforeTax] => 49.00
[Taxes] => Array
(
[Amount] => 0.00
)

)
)

)



I know there's something very basic that I'm not understanding about the relationship of objects and arrays, but for the life of me I can't figure this out.




1 Answer
1



This code will get you nearer to what you say you need.


$AdditionalChargeINS = array(
'Quantity' => '1',
'ChargeTemplateID' => "ChargeTemplateID_INS",
'Amount' => array(
'AmountBeforeTax' => "fee_insurance",
'Taxes' => array(
'Amount' => '0.00'
)
)
);

$AdditionalChargeDAM = array(
'Quantity' => '1',
'ChargeTemplateID' => "ChargeTemplateID_DAM",
'Amount' => array(
'AmountBeforeTax' => "fee_damage",
'Taxes' => array(
'Amount' => '0.00'
)
)
);
$arr1["additionalCharge"] = (object)$AdditionalChargeDAM;
$arr2["additionalCharge"] = (object)$AdditionalChargeDAM;
$arr = (object)array((object)$arr1,(object)$arr2);

print_r($arr);



Produces this output...


stdClass Object
(
[0] => stdClass Object
(
[additionalCharge] => stdClass Object
(
[Quantity] => 1
[ChargeTemplateID] => ChargeTemplateID_DAM
[Amount] => Array
(
[AmountBeforeTax] => fee_damage
[Taxes] => Array
(
[Amount] => 0.00
)
)
)
)

[1] => stdClass Object
(
[additionalCharge] => stdClass Object
(
[Quantity] => 1
[ChargeTemplateID] => ChargeTemplateID_DAM
[Amount] => Array
(
[AmountBeforeTax] => fee_damage
[Taxes] => Array
(
[Amount] => 0.00
)
)
)
)
)



This is closer to what you need and maybe enough to get you going in the right direction. At least with this structure you can iterate through your object list and do whatever processing you need,



The main problem with your stated desired output is that it contained an associative array with duplicate key 'additionalCharge', which PHP does not allow :-)





@enigma65.0 if above answers your question, please accept/upvote. Otherwise feel free to ask for more clarification :-)
– bcperth
Aug 12 at 23:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard