我想用PHP的foreach循环解析一个数组,以获取'ques'数组中的对象名称和值。我想要
[
{
"ques": [
{
"name": "comment",
"value": "comment me for the reason",
"sur_id": "1",
"user_id": "admin@gmail.com",
"pagename": "question_response"
},
{
"name": "check-box[]",
"value": "1"
},
{
"name": "radio",
"value": "radio 2"
},
{
"name": "yes",
"value": "no"
}
]
"ques":[
{
"name": "date",
"value": "2015-10-23"
"user_id": "admin1@gmail.com",
},
{
"name": "select-deopdown",
"value": ""
},
{
"name": "true",
"value": "false"
},
{
"name": "number",
"value": "55"
}
]
}
]
我想将名称,值和user_id与'ques'数组分开:
while ($fetch = mysql_fetch_array($query1)) {
$content = $fetch['CONTENT_VALUES'];
// print_r($content);
$content_value= mb_convert_encoding($content ,"UTF-8");
$datas = json_decode($content, true);
foreach($datas->ques as $values)
{
echo $values->value . "\n";
print_r($values);
}
$test[] = array('ques' => $datas ,'answer'=>$values);
}
答案 0 :(得分:6)
如果要为每个值创建三个不同的数组,则创建三个空白数组,然后在foreach中将相应的值存储到它们中。我给你一个常见的例子
$name = array(); $values= array(); $users = array(); foreach($datas->ques as $values) {
$name[] = $values->name; $values[] = $values->value; $users[] = !empty($values->user_id) ? $values->user_id : '';
}
并且您可以根据需要进行修改。
谢谢。
答案 1 :(得分:1)
var my_layer = boxLayer;
if (my_layer instanceof OpenLayers.Layer.Vector && my_layer.features.length == 0)
{
console.log('Object not ready :( ')
return;
}
console.log('projection (srsName) =', my_layer.features[0].layer.projection.getCode());//"EPSG:3857"
layer['format'] = new OpenLayers.Format.GML();
console.log('OpenLayers.Format.GML:\n ',my_layer.format.write(my_layer.features[0]));//GML string without srsName
答案 2 :(得分:1)
This could help
$data = json_decode($json);
$result = [];
foreach($data as $row)
{
foreach($row as $k => $v)
{
$i = 0;
foreach($v as $key => $val)
{
foreach($val as $k1 => $v1)
{
$result[$i][$k1] = $v1;
}
$i++;
}
}
}
echo json_encode($result);
答案 3 :(得分:0)
我通过以下代码解决了这个问题。感谢他们中的3个人通过提供想法来指导。
$datas = json_decode($content, true);
foreach($datas as $values)
{
$name = $values['name'];
$value = $values['value'];
$user_id = $values['user_id'];
$test[]= array('user'=>$user,'name'=>$name,'value'=>$value);
}
}