这个问题是来自的后续问题this https://stackoverflow.com/questions/17112360/php-multi-dimension-array/17112404?noredirect=1#comment24760136_17112404现在,对于我的后续问题,我在页面上也有这个对象:
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 https://stackoverflow.com/questions/17112360/php-multi-dimension-array/17112404?noredirect=1#comment24760136_17112404=> 注册对象如何获取所有名字和姓氏?
感谢大家的兴趣和时间。
亲切的问候
克里斯
比其他答案更进一步解释您的情况。
这里有一个(注册者)对象数组。这实际上是一个索引为 0 - 1 的关联数组(与所有 PHP 数组一样)。
$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 loop:
for ($i = 0, $len = count($registrantObjects); $i < $len; $i++) {
// $registrantObjects[$i] gives a Registrant object
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)