2011-01-10 14 views
5

Eventuali duplicati:
How to access object properties with names like integers?Come per accedere alla proprietà php ricci oggetto doppietta

print_r($myObj) ha pronunciato la seguente risultato:

stdClass Object 
(
    [4021450] => stdClass Object 
    (
     [property1] => ooo 
     [property2] => xxx 
    ) 
    [3971601] => stdClass Object 
    (
     [property1] => 123 
     [property2] => 356 
    ) 
) 

Come posso utilizzare la sintassi parentesi graffa con variabile per accedere allo sub-object?

ho provato:

$myObj->'3971601';      // Parse error: syntax error 
$myObj->{'3971601'};     // Works 
$id = 3971601; $myObj->{$id};   // Notice: Trying to get property of non-object 
$id = 3971601; $myObj->{''.$id};  // Notice: Trying to get property of non-object 
$arr = (array)$myObj; $arr[3971601]; // Notice: Undefined offset: 3971601 
$arr = (array)$myObj; $arr['3971601']; // Notice: Undefined index: 3971601 

risposta

3

si dovrebbe essere in grado di omettere le parentesi graffe del tutto: $myObj->$id. I tuoi ultimi 4 esempi, tuttavia, indicano che qualcosa non va. Sembra che da qualche parte lungo le linee, $myObj sia stato impostato null o in qualche altro valore non di oggetto.