Jump to content

[SOLVED] need help access a property inside an object that I get from an API


simplemedia

Recommended Posts

Hi all, I'm having trouble parsing some information I'm getting from the Basecamp API. But I think my problem is just not knowing some basic PHP approaches to handling objects. Any help is appreciated!

 

I have an object called $list that is return from the API. I can print_r and view this object. It appears to have a property called (somewhat misleading) 'todo-item', which contains an array called 'todo-items'.

 

This todo-lists array is what I want, but how do I get to it?

 

I tried using $list->todo-item but this didn't work. Is it because of the dash in the property name?

 

Any help is much appreciated!

 

SM

P.S. Here is what I get when I print_r($list). Thanks again!

 

stdClass Object

(

    [completed-count] => 0

    [description] =>

    [id] => 5351227

    [milestone-id] =>

    [name] => test list

    [position] => 6

    [private] => false

    [project-id] => 2832599

    [tracked] => false

    [uncompleted-count] => 13

    [todo-items] => stdClass Object

        (

            [todo-item] => Array

                (

                    [0] => stdClass Object

                        (

                            [completed] => false

                            [content] => test task #0

                            [created-on] => 2009-01-28T08:17:14Z

                            [creator-id] => 237175

                            [id] => 31112734

                            [position] => 1

                            [todo-list-id] => 5351227

                        )

 

                    [1] => stdClass Object

                        (

                            [completed] => false

                            [content] => test task #1

                            [created-on] => 2009-01-28T08:17:17Z

                            [creator-id] => 237175

                            [id] => 31112736

                            [position] => 2

                            [todo-list-id] => 5351227

                        )

 

                    [2] => stdClass Object

                        (

                            [completed] => false

                            [content] => test task #2

                            [created-on] => 2009-01-28T08:17:20Z

                            [creator-id] => 237175

                            [id] => 31112738

                            [position] => 3

                            [todo-list-id] => 5351227

                        )

 

                    [3] => stdClass Object

                        (

                            [completed] => false

                            [content] => test task #3

                            [created-on] => 2009-01-28T08:17:23Z

                            [creator-id] => 237175

                            [id] => 31112739

                            [position] => 4

                            [todo-list-id] => 5351227

                        )

                )

        )

 

    [complete] => false

)

 

I tried this:

 

<? $list_item = $list->todo-items->todo-item[2]; ?>

 

But I get this error:

 

Parse error: syntax error, unexpected T_OBJECT_OPERATOR on line 31

 

I'm stumped.  Both $list->todo-items and $list->todo-item return null.

 

You did make me realize I have my object names list-item and list-items reversed, thanks. But still not able to access what I need.

 

It would seem that $list->todo-items should return the object that includes the array I need, but I'm getting null back for this....

I have worked on this problem a little more and it appears to be the dashes that are the source of the problem?

 

When I try to access $list->id it works fine.  But when I try to access $list->project-id I get a null value back.  Both properties should be there.  But the ones with dashes in the names I am not able to access?

 

So I guess my real question is:  if you access an object through an API and the object has dashes in its property names, how do you access those properties?

 

Any help is very much appreciated!

 

SM

Try to define the names with a dash as variables, and use them instead. Guess you can't use dashes there, since they are part of the -> operator.

 

<?php
$a = 'todo-items';
$b = 'todo-item';
$list_item = $list->$a->{$b}[2];
?>

 

I'm not sure how the $b[2] part will act though, so I put some brackets around the variable. Maybe that'll do it.

Ahh, that's clever ;)

 

But you know what one of Kernighan's Laws says about being clever:

"Debugging is twice as hard as writing the program, so if you write the program as cleverly as you can, by definition, you won’t be clever enough to debug it."

 

It would simplify things, especially for people new to your code, to change the attribute names to a cleaner name (sans-dash) than to always access them with braces.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.