doubei3312 2013-06-14 15:45
浏览 35
已采纳

php多维数组?

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
                        (
                        )

                )

        )

)

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

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?

  • 写回答

3条回答 默认 最新

  • doutao8774 2013-06-14 15:47
    关注

    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?

    • 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算