simplemedia Posted April 2, 2009 Share Posted April 2, 2009 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 Link to comment https://forums.phpfreaks.com/topic/152274-solved-need-help-access-a-property-inside-an-object-that-i-get-from-an-api/ Share on other sites More sharing options...
simplemedia Posted April 2, 2009 Author Share Posted April 2, 2009 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 ) Link to comment https://forums.phpfreaks.com/topic/152274-solved-need-help-access-a-property-inside-an-object-that-i-get-from-an-api/#findComment-799662 Share on other sites More sharing options...
genericnumber1 Posted April 2, 2009 Share Posted April 2, 2009 That is a tad confusing. Try $list->todo-items->todo-item; To access the array, then you can add the index to the end to access certain elements of the array. Eg. $list->todo-items->todo-item[2]; Link to comment https://forums.phpfreaks.com/topic/152274-solved-need-help-access-a-property-inside-an-object-that-i-get-from-an-api/#findComment-799663 Share on other sites More sharing options...
simplemedia Posted April 2, 2009 Author Share Posted April 2, 2009 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.... Link to comment https://forums.phpfreaks.com/topic/152274-solved-need-help-access-a-property-inside-an-object-that-i-get-from-an-api/#findComment-799681 Share on other sites More sharing options...
simplemedia Posted April 2, 2009 Author Share Posted April 2, 2009 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 Link to comment https://forums.phpfreaks.com/topic/152274-solved-need-help-access-a-property-inside-an-object-that-i-get-from-an-api/#findComment-799749 Share on other sites More sharing options...
thebadbad Posted April 2, 2009 Share Posted April 2, 2009 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. Link to comment https://forums.phpfreaks.com/topic/152274-solved-need-help-access-a-property-inside-an-object-that-i-get-from-an-api/#findComment-799803 Share on other sites More sharing options...
simplemedia Posted April 2, 2009 Author Share Posted April 2, 2009 that did it! thank you very much! Link to comment https://forums.phpfreaks.com/topic/152274-solved-need-help-access-a-property-inside-an-object-that-i-get-from-an-api/#findComment-799813 Share on other sites More sharing options...
genericnumber1 Posted April 3, 2009 Share Posted April 3, 2009 To save the hassle you can also group via braces $list->{todo-items}->{todo-item}[2]; Link to comment https://forums.phpfreaks.com/topic/152274-solved-need-help-access-a-property-inside-an-object-that-i-get-from-an-api/#findComment-800031 Share on other sites More sharing options...
thebadbad Posted April 3, 2009 Share Posted April 3, 2009 To save the hassle you can also group via braces $list->{todo-items}->{todo-item}[2]; Ahh, that's clever Link to comment https://forums.phpfreaks.com/topic/152274-solved-need-help-access-a-property-inside-an-object-that-i-get-from-an-api/#findComment-800228 Share on other sites More sharing options...
genericnumber1 Posted April 3, 2009 Share Posted April 3, 2009 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. Link to comment https://forums.phpfreaks.com/topic/152274-solved-need-help-access-a-property-inside-an-object-that-i-get-from-an-api/#findComment-800605 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.