phpobject

How to access specific first level values in a multidimensional object?


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 \r\n<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?


Solution

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