我该如何阅读php [{“profile”:“Admin”,“Cpt_profile”:“3”},{“profile”:“Consultant”,“Cpt_profile”:“1”}]
问题描述:
我怎么能读这我该如何阅读php [{“profile”:“Admin”,“Cpt_profile”:“3”},{“profile”:“Consultant”,“Cpt_profile”:“1”}]
[{"profile":"Admin","Cpt_profile":"3"},
{"profile":"Consultant","Cpt_profile":"1"}]
在PHP请我希望有,
profile : admin
cpt_profile : 3
和
profile :consultant
cpt_profile :1
答
的字符串是JSON,只需使用json_decode()返回一个数组,像这样:
$json = '[{"profile":"Admin","Cpt_profile":"3"},{"profile":"Consultant","Cpt_profile":"1"}]';
$data = json_decode($json, true);
print_r($data);
// Will return
// Array
// (
// [0] => Array
// (
// [profile] => Admin
// [Cpt_profile] => 3
// )
// [1] => Array
// (
// [profile] => Consultant
// [Cpt_profile] => 1
// )
//)
答
如前所述。
<?php
$json = json_decode('[{"profile":"Admin","Cpt_profile":"3"},{"profile":"Consultant","Cpt_profile":"1"}] ',true);
// print_r($json);
foreach($json as $j)
{
print_r($j);
print 'profile:'.$j['profile']."\r\n";
print 'Cpt_profile:'.$j['Cpt_profile']."\r\n\r\n";
}
?>
要拨打的每一行使用 “的foreach” 如上图所示
+0
感谢您的帮助,我做到了这一点,它工作正常 $ value){?> profile。$ value-> Cpt_profile; ?> \t –
看起来像一个JSON:http://php.net/manual/en/function.json-decode.php – CD001
这是一个JSON字符串,使用['json_decode()'](http://php.net/manual/en/function.json-decode.php)将其转换为PHP可用数据类型 – RiggsFolly