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.
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 :-)