如何用一个值检索所有对象数组?

时间:2013-06-14 16:29:22

标签: php object

这个问题是this的后续问题。现在,对于我的跟进问题,我在页面上也有这个对象:

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] =&gt;中检索第一个姓氏和名字。注册人对象不是来自1 =&gt;注册人对象如何获得所有的名字和姓氏? 感谢所有人的兴趣和时间。 亲切的问候 克里斯

3 个答案:

答案 0 :(得分:3)

让我们说你打印的对象是&#34; $ RegistrantObjects&#34;

您可以执行以下操作:

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

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

答案 1 :(得分: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
}

答案 2 :(得分:1)

尝试以下代码;

<?php 
    foreach($Registrant as $reg) {
        echo $reg->firstname;
        echo $reg->lastname;
    }
?>