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

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

本文介绍了如何用一个值检索所有对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This question is a follow up from this Now for my follow up question I also have this object on page:

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] =>
    )

所以按照相同的理论,我正在检索这样的值:

So following the same theory I am retriving the values like this:

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

但这只会从 [0] => 注册对象中检索第一个姓氏和名字,而不是从 1 => Registrant Object 如何获取所有名字和姓氏?感谢大家的兴趣和时间.亲切的问候克里斯

but this only retrieves the first lastname and firstname from [0] => Registrant Object not from 1 => Registrant Object how to i get all of the first names and last names?Thank everyone for there interest and there time.Kind regardsChris

推荐答案

比其他答案更深入地解释您的场景.

To explain your scenario a bit further than other answers.

这里有一组(注册人)对象.这实际上是一个关联数组(与所有 PHP 数组一样),索引范围为 0 - 1.

You have an array of (Registrant) objects here. This is actually an associative array (as all PHP arrays) with indices from 0 - 1.

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

您可以同时访问它们.但是如果你想迭代数组(即遍历所有元素并对每个元素都做同样的事情),你应该使用循环.对于这个用例,PHP 有一个很好的 foreach 循环:

You can access them both. But if you want to iterate the array (i.e. going over all elements and do the same for each one), you should use a loop.PHP has a nice foreach loop for this use case:

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 循环:

And both loops are equal to this for loop:

for ($i = 0, $len = count($registrantObjects); $i < $len; $i++) {
  // $registrantObjects[$i] gives a Registrant object
}

这篇关于如何用一个值检索所有对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:07