有这个文字
"号码":"X0011","姓名":"aaaaaaaaaaaaaa","地址":"12345","电话":"56976585","号码":"X0044","姓名":"bbbbbbbbbbbbbbbbbb","地址":"786487647","电话":"34214234",.......
并想用 PHP 像这样返回(只是名称和代码,忽略所有其他字段)
编号:X0011
姓名:啊啊啊啊啊啊
编号:X0044
名称:bbbbbbbbbbbbbbbbbbbb
……
提前致谢
以下代码将允许您循环浏览所有项目并获取各个值$temp_str[1]
将包含该项目的实际值。这也意味着您可以继续向字符串添加信息,并且此代码将对其进行解析。
$str = '"Number":"X0011","Name":"aaaaaaaaaaaaaa","Adress":"12345","Phone":"56976585"';
$str = str_replace('"',"",$str); // remove the double quotes
$pieces = explode(",", $str);
foreach($pieces as $piece){
$temp_str = explode(',',$piece);
echo $temp_str[0]; // 'Number:'X0011 // gives the heading
echo $temp_str[1]; // Name:'aaaaaaa' // gives the value
echo "<br />";
}
您可以使用爆炸功能做到这一点
========
$mystring = ""Number":"X0011","Name":"aaaaaaaaaaaaaa","Adress":"12345","Phone":"56976585";
$pieces = explode(",", $mystring);
echo $pieces[0]; // Number:X0011
echo $pieces[1]; // Name:aaaaaaa