【JS】PHP的序列化和反序列化使用方法

今天写一个小程序,需要从数据库获取客户的订单配送地点,数据表里的address字段内容却不是单独的省市区标准文字,而是一串类似于{"key":"value"}结构的有规律的字符串,要获取其中的一些key返回客户端。

字符串如下:
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/1791297747...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...
https://xueqiu.com/8951139056...

a:17:{s:2:"id";s:3:"145";s:7:"uniacid";s:1:"1";s:6:"openid";s:35:"Xe7fgaBJLuoGKJ543qwZCVdFgqojL8tgh";s:8:"realname";s:6:"张三";s:6:"mobile";s:11:"12345678900";s:8:"province";s:9:"北京市";s:4:"city";s:12:"北京辖区";s:4:"area";s:9:"西城区";s:7:"address";s:18:"新港中路397号";s:9:"isdefault";s:1:"1";s:7:"zipcode";s:0:"";s:7:"deleted";s:1:"0";s:6:"street";s:0:"";s:9:"datavalue";s:14:"null null null";s:15:"streetdatavalue";s:0:"";s:3:"lng";s:0:"";s:3:"lat";s:0:"";}

一开始我想过用json_encode方法去转化,但是得到的结果不是想要的,这里就不贴出来,因为没有系统学过PHP的开发,只是略微了解常用的操作,于是询问项目组的大佬,看了会就回我信息:

【JS】PHP的序列化和反序列化使用方法

然后搜索这个,可能是大佬没有记得这个单词怎么拼写,搜索的结果却让我茅塞顿开

【JS】PHP的序列化和反序列化使用方法

直接上代码

1. $SQl = "SELECT * FROM orders WHERE status = 5;";

2. $res = $con->query($SQl);

3. $jsonarr = array();

4. while($row = mysqli_fetch_array($res,MYSQL_ASSOC)){

5. $count = count($row);

6. for ($i = 0 ;$i <$count;$i++){

7. unset($row[$i]);

8. }

9. array_push($jsonarr,unserialize($row['address']));

10. }

11. echo(json_encode($jsonarr));

最后顺利获取想要的结果

【JS】PHP的序列化和反序列化使用方法

以上是 【JS】PHP的序列化和反序列化使用方法 的全部内容, 来源链接: utcz.com/a/96283.html

回到顶部