How do I access this object property with an illegal name?

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

echo $x->{'todo-list'};

Another possibility:

$todolist = 'todo-list';
echo $x->$todolist;

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;
}

Try this simplest way!

$obj = $myobject->{'mydash-value'};
$objToArray = array($obj);

Tags:

Php

Object