php多维数组?

时间:2013-06-14 15:45:23

标签: php arrays

我正在从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隐藏其余部分但是我确信我只是一个白痴而且那里是一种更简单的遍历此数组的方法,因此它只会吐出我需要的两个值。 我该怎么做呢?

3 个答案:

答案 0 :(得分:5)

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

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

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

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

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

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

答案 1 :(得分:2)

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

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

答案 2 :(得分: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/>";
    ?>

?还是我太简单了?