0

第一个数组是位置,第二个是汽车及其相关价格。我怎样才能像下面的示例一样组合它们?

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

有人可以告诉该代码的问题吗?

4

2 回答 2

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

于 2012-05-24T12:39:04.723 回答
0

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

于 2012-05-24T12:54:11.017 回答