如何组合两个SimpleXML元素数组?

时间:2012-05-24 12:32:05

标签: php xml soap

第一个阵列是位置,第二个是具有相关价格的汽车。如何将它们组合起来,如下图所示?

Array
(
    [0] => SimpleXMLElement Object
        (
            [companyLocationInfo] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [companyName] => AVIS
                            [name] => NYCC07
                            [line1] => 420 EAST 90TH STREET
                        )
                    [2] => SimpleXMLElement Object
                        (
                            [companyName] => AVIS
                            [name] => NYCC06
                            [line1] => 310 EAST 64TH STREET
                        )
                    [3] => SimpleXMLElement Object
                        (
                            [companyName] => AVIS
                            [name] => NYCC01
                            [line1] => 68 EAST 11TH STREET
                        )

                )

            [rates] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [companyName] => AVIS
                            [name] => NYCC07
                            [vehicleRentalPrefType] => CCAR
                            [rateAmount] => 83.99
                            [rateCurrency] => USD
                        )
                    [2] => SimpleXMLElement Object
                        (
                            [companyName] => AVIS
                            [name] => NYCC06
                            [vehicleRentalPrefType] => CCAR
                            [rateAmount] => 110.54
                            [rateCurrency] => USD
                        )
                    [3] => SimpleXMLElement Object
                        (
                            [companyName] => AVIS
                            [name] => NYCC01
                            [vehicleRentalPrefType] => CCAR
                            [rateAmount] => 210.65
                            [rateCurrency] => USD
                        )

                )

        )

)

我想像这样把它们结合起来:

AVIS 420 EAST 90TH STREET
CCAR 83.99 USD

AVIS 310 EAST 64TH STREET
CCAR 110.54 USD

AVIS 68 EAST 11TH STREET
CCAR 210.65 USD

使用PHP如何将它组合起来?


我刚刚得到了我的问题的答案,但我无法正常工作,我想结合阵列,第一个是位置,第二个是汽车,但在第二个是另一个阵列我必须将值与第一个数组匹配,

以下是一个例子,

Array
(
    [0] => SimpleXMLElement Object
        (
            [companyLocationInfo] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [companyName] => AVIS
                            [name] => NYCC07
                            [line1] => 420 EAST 90TH STREET
                        )
                    [2] => SimpleXMLElement Object
                        (
                            [companyName] => AVIS
                            [name] => NYCC06
                            [line1] => 310 EAST 64TH STREET
                        )
                    [3] => SimpleXMLElement Object
                        (
                            [companyName] => AVIS
                            [name] => NYCC01
                            [line1] => 68 EAST 11TH STREET
                        )

                )

            [rates] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [pickupDropoffLocations] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                            [companyName] => AVIS
                                            [name] => NYCC07
                                        )
                                )
                            [vehicleRentalPrefType] => CCAR
                            [rateAmount] => 83.99
                            [rateCurrency] => USD
                        )
                    [2] => SimpleXMLElement Object
                        (
                            [pickupDropoffLocations] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                            [companyName] => AVIS
                                            [name] => NYCC06
                                        )
                                )
                            [vehicleRentalPrefType] => CCAR
                            [rateAmount] => 110.54
                            [rateCurrency] => USD
                        )
                    [3] => SimpleXMLElement Object
                        (
                            [pickupDropoffLocations] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                            [companyName] => AVIS
                                            [name] => NYCC01
                                        )
                                )
                            [vehicleRentalPrefType] => CCAR
                            [rateAmount] => 210.65
                            [rateCurrency] => USD
                        )

                )

        )

)

这是我正在使用的代码,但不起作用,

$results_array = array();

foreach($result[0]->rates as $rate) {
    foreach($result[0]->companyLocationInfo as $info) {
        if($info->name == $rate->pickupDropoffLocations[0]->name) {
            $results_array[] = array(
                'line1' => $info->line1,
                'name' => $info->locationDetails->name,
                'companyName' => $info->companyName,
                'vehicleRentalPrefType' => $rate->vehicleRentalPrefType
            );
        }
    }
}
print_r($results_array);

我希望它看起来像:

AVIS 420 EAST 90TH STREET
CCAR 83.99 USD

AVIS 310 EAST 64TH STREET
CCAR 110.54 USD

AVIS 68 EAST 11TH STREET
CCAR 210.65 USD

有人能说出该代码的问题吗?

2 个答案:

答案 0 :(得分:0)

结合......这样的事情,或许?

$results = array();

foreach($array[0]->companyLocationInfo as $info) {
    foreach($array[0]->rates as $rate) {
        if($info->name == $rate->name) {
            $results[] = array(
                'line1' => $info->line1,
                'name' => $info->name,
                'companyName' => $info->companyName,
                'vehicleRentalPrefType' => $rate->vehicleRentalPrefType
            );
        }
    }
}

然后为你循环$results

答案 1 :(得分:0)

尝试数组对象(http://php.net/arrayobject)插入它:)