我有一个复杂的JSON,我需要从这个JSON创建数组。 我已经解析了JSON并创建了这样的变量:
def calculate(something)
$memo[something] ||= _calculate(something)
end
def _calculate(something)
return if something == 'bar'
perform_calculation(something) # or maybe inline this then
end
现在我需要一个带有这种变量的数组,其中 $name = $json[response][docs][$i][name][0];
$osm_id = $json[response][docs][$i][osm_id][0];
$place = $json[response][docs][$i][place][0];
$population= $json[response][docs][$i][population][0];
正在发生变化,如下所示:
$i
你能帮助我完成这个阵列的循环吗?
答案 0 :(得分:1)
如果我的理解是正确的,
$expected_arr = array();
foreach($json[response][docs] as $inc => $values){
$data = array();
foreach($values as $key => $val){
$data[$key] = $val[0];
}
$expected_arr[$inc] = $data;
}
所以你会得到像
这样的东西array(0 => array( 'name'=>'xxx', 'osm_id'=>'yy',..), 1=> array('name'=>'',.. ,),...)