从字符串导出数据

时间:2016-06-03 07:35:29

标签: php string export preg-replace extract

我有以下字符串:

{"id":3661917,"mId":2408699,"isR":false,"opps": [{"id":37794066,"optN":1,"wn":false,"rt":"1.24","fau":null},{"id":37794067,"optN":2,"wn":false,"rt":"1.01","fau":null},{"id":37794068,"optN":3,"wn":false,"rt":"5.40","fau":null},{"id":37794069,"optN":4,"wn":false,"rt":"3.36","fau":null},{"id":37794070,"optN":5,"wn":false,"rt":"11.40","fau":null}],"oTpId":7}

我如何获得值:“optN”(例如“optN”:1)和“rt”(例如“rt”:“1.24”)?我不知道,谢谢。

3 个答案:

答案 0 :(得分:1)

这是一个JSON字符串。只需使用these instructions函数对其进行解码,并将项目作为数组访问。例如:

$data = json_decode($jsonString, true);
echo $data['opps'][0]['optN']; // print first opps element optN value
echo $data['opps'][0]['rt']; // print first opps element rt value

答案 1 :(得分:0)

此字符串为JSON。您可以使用json_decode将其转换为PHP对象。

$your_string = '{"id":3661917,"mId":2408699,"isR":false,"opps": [{"id":37794066,"optN":1,"wn":false,"rt":"1.24","fau":null},{"id":37794067,"optN":2,"wn":false,"rt":"1.01","fau":null},{"id":37794068,"optN":3,"wn":false,"rt":"5.40","fau":null},{"id":37794069,"optN":4,"wn":false,"rt":"3.36","fau":null},{"id":37794070,"optN":5,"wn":false,"rt":"11.40","fau":null}],"oTpId":7}';
$object = json_decode( $your_string );

答案 2 :(得分:0)

json字符串,您可以按json_decode解码json字符串,因此请使用

$json = '{"id":3661917,"mId":2408699,"isR":false,"opps": [{"id":37794066,"optN":1,"wn":false,"rt":"1.24","fau":null},{"id":37794067,"optN":2,"wn":false,"rt":"1.01","fau":null},{"id":37794068,"optN":3,"wn":false,"rt":"5.40","fau":null},{"id":37794069,"optN":4,"wn":false,"rt":"3.36","fau":null},{"id":37794070,"optN":5,"wn":false,"rt":"11.40","fau":null}],"oTpId":7}';
$json_array = json_decode($json,true);
if(isset($json_array['opps']) && $json_array['opps']!= false)
{
    foreach($json_array['opps'] as $jArray)
    {
        echo " optN = " .$jArray['optN'];
        echo "<br/>";
        echo " rt = " .$jArray['rt'];
        echo "<br/>";echo "<br/>";

    }
}