问题描述
我检索来自API,多维数组PHP(我认为)现在所有的值返回完美,当我转储阵列的print_r我得到这样的:
I am retrieving a multidimensional php array (I think) from an API, now all of the values return perfectly and when I dump the array with print_r I get this:
Event Object
(
[title] => test
[link] => google.com
[updated] => 2013-03-06T12:08:56.521-05:00
[id] => test
[name] => Copy of Copy of Copy of Mar 05, 2013 - TEST4
[description] =>
[registered] => 2
[createdDate] => 2013-03-06T12:08:56.521-05:00
[status] => COMPLETE
[eventType] => OTHER
[eventLocation] => EventLocation Object
(
[location] => test
[addr1] => test
[addr2] =>
[addr3] =>
[city] => madrid
[state] => andalucia
[country] =>
[postalCode] => 06103
)
[registrationUrl] => https://google.com
[startDate] => 2013-03-07T13:00:00-05:00
[endDate] => 2013-03-07T13:00:00-05:00
[publishDate] => 2013-03-06T12:11:15.958-05:00
[attendedCount] => 0
[cancelledCount] => 0
[eventFeeRequired] => FALSE
[currencyType] => USD
[paymentOptions] => Array
(
)
[registrationTypes] => Array
(
[0] => RegistrationType Object
(
[name] =>
[registrationLimit] =>
[registrationClosedManually] =>
[guestLimit] =>
[ticketing] =>
[eventFees] => Array
(
)
)
)
)
现在通过机智装模作样的我基本的PHP我发现,我可以列出所有的第一阵列项目从[标题]到[EVENTTYPE]是这样的:
Now bumbling through wit my basic PHP i have found that i can list all of the first array items from [title] to [eventType] like this:
<?php
// get details for the first event returned
$Event = $ConstantContact->getEventDetails($events['events'][0]);
reset($Event);
while (list($key, $value) = each($Event)) {
echo "$key => $value \r\n<br/>";
}
?>
我的问题:所有我需要做的是检索[标题]和[的startDate]我不需要休息,现在我可以用js和css只是躲在休息,但我相信我只是作为一个白痴,有是遍历数组所以它只吐出来的是两个值我需要一个更简单的方法。
我该怎么做呢?
my question: All I need to do it retrieve [title] and [startDate] I don't need the rest now I could just hide the rest using Js and css but i am sure i am just being an idiot and there is an easier way to traverse this array so it only spits out the two values i need.How do i do this?
推荐答案
您不必遍历整个对象。只需输入您想要的属性:
You do not have to traverse the whole object. Just access the properties you want:
$title = $Event->title;
$startDate = $Event->startDate;
// or
echo $Event->title;
echo $Event->startDate;
它实际上是一个对象 - 不可以的(关联)数组!
有什么区别?
It's actually an object - not an (associative) array!
What's the difference?
-
对象是类的一个实例。一个类的方法和属性(成员变量)。
与C ++或其他一些OOP语言,可以动态地定义属性,无需在类声明,宣布他们。
An object is an instance of a class. A class has methods and attributes (member variables).
Unlike C++ or some other OOP languages, you can define attributes dynamically without declaring them in the class declaration.
数组是简单的按键和值的容器。
An array is simply a container for keys and their values.
这篇关于PHP的多维度阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!