1

我正在从 API 检索一个多维 php 数组(我认为),现在所有值都完美返回,当我用 print_r 转储数组时,我得到了这个:

 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,我发现我可以列出从 [title] 到 [eventType] 的所有第一个数组项,如下所示:

<?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/>";
    }
    ?>

我的问题:所有我需要做的就是检索 [title] 和 [startDate] 我现在不需要其余的我可以使用 Js 和 css 隐藏其余的但我确信我只是一个白痴并且有一个更容易遍历这个数组的方法,所以它只吐出我需要的两个值。我该怎么做呢?

4

3 回答 3

5

您不必遍历整个对象。只需访问您想要的属性:

$title = $Event->title;
$startDate = $Event->startDate;

// or
echo $Event->title;
echo $Event->startDate;

它实际上是一个对象 -而不是(关联)数组!
有什么不同?

  • 对象是类的一个实例。一个类有方法和属性(成员变量)。
    与 C++ 或其他一些 OOP 语言不同,您可以动态定义属性,而无需在类声明中声明它们。

  • 数组只是键及其值的容器。

于 2013-06-14T15:47:30.147 回答
2

它似乎不是一个数组,而是一个对象,所以是这样的:

echo $Event->title;
echo $Event->startDate;
于 2013-06-14T15:48:51.213 回答
1

是吗 ...

<?php 
    // get details for the first event returned
    $Event = $ConstantContact->getEventDetails($events['events'][0]);
     reset($Event);
     echo $Event->$title . "<br/>";
     echo $Event->$startDate . "<br/>";
    ?>

? 还是我太简单了?

于 2013-06-14T15:50:25.670 回答