-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] => Registrant Object 而不是从1 => Registrant Object 检索第一个姓氏和名字我如何获取所有名字和姓氏?感谢大家的兴趣和时间。亲切的问候克里斯

4

3 回答 3

3

假设您打印的对象是“$RegistrantObjects”

您可以执行以下操作:

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

在 foreach 中,$registrant 对象的访问方式与您的代码访问它的方式相同。

于 2013-06-14T16:38:28.253 回答
3

比其他答案更进一步地解释您的情况。

您在这里有一个 (Registrant) 对象数组。这实际上是一个关联数组(与所有 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 回答