Referencing Json Elements Dynamically

In my code I request a json and decode it to an array.

$obj = $this->getJson($url, $auth, $params);

I can reference the count of records by using the following:

$count = $obj->response->count;

When processing the array I would like to dynamically reference the response structure.

 for ($i=0 ; $i<$num_elements; $i++) {

 $arraydata[$i+$rec] = array(

 "id" => $obj->response->$element[$i]->id,

 "name" => $obj->response->$element[$i]->name


 } //inner loop

$element would be the resource name of the json response.

It seems Yii does not process the variable in time and this fails.

Anyone have any suggestions?

Thanks in Advance !