corrected following Rob's suggestion

but still had to cast also 
$data[$name] = (array) $data[$name];