从get url中传递的数组中获取值

时间:2015-03-06 11:09:52

标签: php arrays

我正在制作一个Web服务,其中设置数据在数组

中传递
[{"privacy_id":"74","privacy_selected_option":"public"},{"privacy_id":"77","privacy_selected_option":"public"},{"privacy_id":"177","privacy_selected_option":"public"}]

现在如何从这些传递的数据中获取值。对于普通数组,我们使用foreach然后轻松获取值。

上述值在user_id=370&settings=[{"privacy_id":"74","privacy_selected_option":"public"},{"privacy_id":"77","privacy_selected_option":"public"},{"privacy_id":"177","privacy_selected_option":"public"}] url内传递。我使用$settings=$_REQUEST['settings']

得到它

2 个答案:

答案 0 :(得分:1)

实际上是json

$settings = json_decode($_REQUEST['settings']);

您将获得一个包含保存属性的对象的数组。 你可以循环它

foreach ($settings as $setting) {
    // $setting->privacy_id...
}

$settings = json_decode($_REQUEST['settings'], true);

foreach ($settings as $setting) {
    // $setting['privacy_id']...
}

答案 1 :(得分:0)

如果需要获取值,可以将其取消序列化,然后像普通对象一样访问它。

$settings = unserialize($_REQUEST['settings']);
//then use foreach to loop through and get the settings.