0

最初の配列は場所で、2 番目の配列は車とそれに関連付けられた価格です。以下のサンプルのように組み合わせるにはどうすればよいですか?

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では、どうすればこのように組み合わせることができますか?


質問に対する回答が得られましたが、正しく機能させることができません。配列に結合したいのですが、最初は場所で、2 番目は車ですが、2 番目には別の配列が必要です。値を最初の配列に一致させます。

以下に例を示します。

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

配列オブジェクトを試してください(http://php.net/arrayobject)挿入してください:)

于 2012-05-24T12:54:11.017 に答える
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 に答える