-1

この質問は、これからのフォローアップです。フォローアップの質問のために、ページにこのオブジェクトもあります。

Array
(
    [registrants] => Array
        (
    [0] => Registrant Object
                (
                    [title] => D C
                    [link] => **********
                    [id] => ***************
                    [updated] => 2013-03-06T12:11:49-05:00
                    [lastName] => C
                    [firstName] => D
                    [email] => *********
                    [personalInformation] => PersonalInformation Object
                        (
                            [cellPhone] => 
                            [label] => 
                            [addr1] => 
                            [addr2] => 
                            [addr3] => 
                            [city] => 
                            [state] => 
                            [postalCode] => 
                            [province] => 
                            [country] => 
                            [phone] => 
                        )

                    [businessInformation] => BusinessInformation Object
                        (
                            [fax] => 
                            [website] => 
                            [blog] => 
                            [company] => 
                            [jobTitle] => 
                            [department] => 
                            [label] => 
                            [addr1] => 
                            [addr2] => 
                            [addr3] => 
                            [city] => 
                            [state] => 
                            [postalCode] => 
                            [province] => 
                            [country] => 
                            [phone] => 
                        )

                    [customInformation1] => Array
                        (
                        )

                    [customInformation2] => Array
                        (
                        )

                    [registrationStatus] => REGISTERED
                    [registrationDate] => 2013-03-06T12:11:49-05:00
                    [guestCount] => 0
                    [paymentStatus] => NA
                    [orderAmount] => 
                    [currencyType] => 
                    [paymentType] => 
                    [costs] => Array
                        (
                        )

                )

            [1] => Registrant Object
                (
                    [title] => Test Test
                    [link] => ****
                    [id] =>  *************
                    [updated] => 2013-03-06T12:47:47-05:00
                    [lastName] => Test
                    [firstName] => Test
                    [email] =>  ***************
                    [personalInformation] => PersonalInformation Object
                        (
                            [cellPhone] => 
                            [label] => 
                            [addr1] => 
                            [addr2] => 
                            [addr3] => 
                            [city] => 
                            [state] => 
                            [postalCode] => 
                            [province] => 
                            [country] => 
                            [phone] => 
                        )

                    [businessInformation] => BusinessInformation Object
                        (
                            [fax] => 
                            [website] => 
                            [blog] => 
                            [company] => 
                            [jobTitle] => 
                            [department] => 
                            [label] => 
                            [addr1] => 
                            [addr2] => 
                            [addr3] => 
                            [city] => 
                            [state] => 
                            [postalCode] => 
                            [province] => 
                            [country] => 
                            [phone] => 
                        )

                    [customInformation1] => Array
                        (
                        )

                    [customInformation2] => Array
                        (
                        )

                    [registrationStatus] => REGISTERED
                    [registrationDate] => 2013-03-06T12:47:47-05:00
                    [guestCount] => 0
                    [paymentStatus] => NA
                    [orderAmount] => 
                    [currencyType] => 
                    [paymentType] => 
                    [costs] => Array
                        (
                        )

                )

        )

        [nextLink] => 
    )

したがって、同じ理論に従って、次のような値を取得しています。

<?php echo $Registrant->lastName; echo $Registrant->firstName; echo $Registrant->email; ?>

しかし、これは最初の姓と名のみを [0] から取得します => 登録者オブジェクト1からではありません => 登録者オブジェクト すべての名と姓を取得するにはどうすればよいですか? 関心と時間をありがとうございました。敬具クリス

4

3 に答える 3

3

印刷したオブジェクトが「$RegistrantObjects」であるとしましょう

次のことができます。

foreach ($RegistrantObjects as $registrant)
{
    echo $registrant->lastName;
}

foreach 内では、コードでアクセスするのと同じ方法で $registrant オブジェクトにアクセスできます。

于 2013-06-14T16:38:28.253 に答える
3

シナリオを他の回答よりも少し詳しく説明します。

ここに (登録者) オブジェクトの配列があります。これは実際には (すべての PHP 配列と同様に) 0 から 1 までのインデックスを持つ連想配列です。

$registrantObjects[0] // would give first Registrant object
$registrantObjects[1] // would give second Registrant object

どちらにもアクセスできます。しかし、配列を反復処理したい場合 (つまり、すべての要素を調べて、各要素に対して同じことを行う場合) は、ループを使用する必要があります。PHP には、このユース ケースに適したforeachループがあります。

foreach ($registrantObjects as $registrant) {
  // $registrant is a Registrant object here
  echo $registrant->lastName;
}

これを試すこともできます:

foreach ($registrantObjects as $index => $registrant) {
  // $registrant is a also Registrant object here
  // But we have a variable $index, too. It represents the current 'key'
  // We have a normal (numbered) array thus the keys are [0..1]

  echo $registrant->lastName;
}

そして、両方のループはforループと同じです:

for ($i = 0, $len = count($registrantObjects); $i < $len; $i++) {
  // $registrantObjects[$i] gives a Registrant object
}
于 2013-06-14T16:45:20.023 に答える
1

以下のコードを試してください。

<?php 
    foreach($Registrant as $reg) {
        echo $reg->firstname;
        echo $reg->lastname;
    }
?>
于 2013-06-14T16:40:51.073 に答える