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 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛