我试图从数组中获取值是我的代码
print_r($new[1]->extra_fields);
结果是
[{"id":"1","value":"compny name"},{"id":"2","value":"Dubai"}, {"id":"3","value":"UAE"},{"id":"4","value":"2014-07-31"},{"id":"5","value":"AED 12000"}]</p>
如何单独获取特定值,如公司名称,迪拜,AED 12000
答案 0 :(得分:1)
你在变量
中有json字符串
foreach(json_decode($new[1]->extra_fields, true) as $field){ // true for assoc value array
print_r($field);
print($field['id']);
print($field['value']);
}
编辑:公司名称
foreach(json_decode($new[1]->extra_fields, true) as $field){ // true for assoc value array
print_r($field);
if($field['id'] == "1"){
$comp_name = $field['value'];break;
}
}
答案 1 :(得分:1)
您需要将其用作
$extra_fields = json_decode($new[1]->extra_fields); // use json_decode, as value is json encoded
foreach($extra_fields as $field){
echo $field->value;
}