A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearHow do I access this object property with a hyphenated name? - page 1
User InfoPosts
How do I access this object property with a hyphenated name?#1
I(m using a PHP class someone wrote to interface with the BaseCamp API.

The particular call I(m doing is to retrieve the items in a todo list, which works fine.

My problem is, I(m not sure how to access just the todo-items property of the object that is returned. Here(s the var_dump of the returned object:

object(stdClass)[6]
public (completed-count( => string (0( (length=1)
public (description( => string (Description String( (length=89)
public (id( => string (12345( (length=7)
public (milestone-id( => string (( (length=0)
public (name( => string (Error Reports( (length=13)
public (position( => string (1( (length=1)
public (private( => string (false( (length=5)
public (project-id( => string (58904( (length=7)
public (tracked( => string (false( (length=5)
public (uncompleted-count( => string (1( (length=1)
public (todo-items( =>
object(stdClass)[3]
public (todo-item( =>
object(stdClass)[5]
public (completed( => string (false( (length=5)
public (content( => string (content string here( (length=133)
public (created-on( => string (2009-04-16T20:33:31Z( (length=20)
public (creator-id( => string (23423( (length=7)
public (id( => string (234( (length=8)
public (position( => string (1( (length=1)
public (responsible-party-id( => string (2844499( (length=7)
public (responsible-party-type( => string (Person( (length=6)
public (todo-list-id( => string (234234( (length=7)
public (complete( => string (false( (length=5)


How can I access the todo-items portion of this object?

posted date: 2009-04-16 15:33:00


Re: How do I access this object property with a hyphenated name?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-16 15:33:01


Re: How do I access this object property with a hyphenated name?#3
<?php$x = new StdClass();$x->{(todo-list(} = (fred(;var_dump($x);So, $object->{(todo-list(} is the sub-object. If you can set it like that, then you can also read it the same way.If you wanted to convert it to an array, which can be a little more easily (ie the obvious $ret[(todo-list(] accessing), this code is taken almost verbatim from Zend_Config and will convert for you.public function toArray(){ $array = array(); foreach ($this->_data as $key => $value) { if ($value instanceof StdClass) { $array[$key] = $value->toArray(); } else { $array[$key] = $value; } } return $array;}

posted date: 2009-04-16 15:39:00


Re: How do I access this object property with a hyphenated name?#4
theeeere we go, that's the syntax i'm looking for. thanks!

posted date: 2009-04-16 15:41:00


Re: How do I access this object property with a hyphenated name?#5
Although that's short and sweet (and what I'd recommend), you can also do this via variables: $todolist='todo-list'; $x->$todolist

posted date: 2010-11-26 00:23:00


Re: How do I access this object property with a hyphenated name?#6
Try this simplest way!$obj = $myobject->{(mydash-value(};$objToArray = array($obj);

posted date: 2014-09-24 04:00:00


Re: How do I access this object property with a hyphenated name?#7
Good answers accompany code samples with an explanation for future readers. While the person asking this question may understand your answer, explaining how you arrived at it could help countless others.

posted date: 2014-09-24 05:39:00


Re: How do I access this object property with a hyphenated name?#8
Very late response, with PHP > 5.5, there are better solutions. Either cast the object to an array, or try get_object_vars().

posted date: 2016-04-09 02:34:00


Re: How do I access this object property with a hyphenated name?#9
Oh this is the REAL answer. I was trying forever to access an object property name with a '.' in it and this cleaned that right up!

posted date: 2016-04-14 11:42:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.