如何使用多个相似元素创建StdClass

时间:2018-08-11 23:53:37

标签: php arrays stdclass

我有这段代码生成传递给SoapClient()的请求。这只是更大的请求对象的一部分。

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

这会在请求中生成。

[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
                                )
                        )
                )
        )
)

但是我需要它以这种方式结束(请注意两个节点都称为“ 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
                    )

            )
        )

)

我知道我对对象和数组之间的关系不了解,但是对于我的一生,我无法弄清楚。

1 个答案:

答案 0 :(得分:0)

此代码将使您更接近所需的内容。

$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);

产生此输出...

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

这更接近您的需求,也许足以使您朝正确的方向前进。至少通过这种结构,您可以遍历对象列表并进行所需的任何处理,

所需输出的主要问题是它包含一个带有重复键“ additionalCharge”的关联数组,PHP不允许这样做:-)