获取JSON数组的值

时间:2011-02-16 09:50:28

标签: php arrays json

这是我拥有的json数组,我使用php的

的json_decode()解码它
{"form":{"fieldsets":[{"fieldset_name":"name_1","datapoints":{"dp_id_1":1,"dp_id_2":4}},{"fieldset_name":"name_2","datapoints":{"dp_id_3":1,"dp_id_4":5}}]}}.

解码后得到的数组是

Array ( 
    [form] => Array ( 
        [fieldsets] => Array ( 
            [0] => Array ( 
                [fieldset_name] => name_1 
                [datapoints] => Array ( 
                    [dp_id_1] => 1 
                    [dp_id_2] => 4 
                ) 
            ) 
            [1] => Array ( 
                [fieldset_name] => name_2 
                [datapoints] => Array ( 
                    [dp_id_3] => 1 
                    [dp_id_4] => 5 
                ) 
            ) 
        ) 
    ) 
) 

现在我想将数据点数组dp_id_1,dp_id_2元素推入一个数组,将dp_id_3,dp_id_4推入其他数组

请帮帮我........

1 个答案:

答案 0 :(得分:4)

简单地遍历数组:

$points = array();

for($array['form']['fieldsets'] as $set) {
    $points = array_merge($points, $set['datapoints']);
}

<强>更新

关于你的编辑,它甚至更简单。用以下物体替换身体:

$points[] = $set['datapoints'];

我建议您阅读有关arrays in PHP的内容,以便更好地了解它们的工作原理。