博主:DongJiang
码龄:7年
等级:LV.22级
文章:631
访问:5233623
博客简介:记录与分享
博客创建时间:2018-04-12
他的博客主页 立即前往

友情链接

php如何将json数据和数组进行转换

来源: 2023-07-02 18:14:22

1、php将json数据转成数组

json_decode()函数将json字符串$json解析为php数组"$decoded_json"。第二个参数"true"表示返回的结果为数组而不是对象。

<?php
  $json = '{"name": "shenghao", "age": 3, "city": "广州"}';
  // 第二个参数用于返回数组代替对象
  $decoded_json = json_decode($json, true); 

// 返回结果为数组:
Array( 'name' => 'shenghao', 'age' => 3, 'city' => '广州');
?>

2、php将数组转成json字符串

json_encode()用于对变量进行JSON编码,该函数如果执行成功返回 JSON 数据,否则返回 FALSE 。

<?php
  $arr = Array( 'name' => 'shenghao', 'age' => 3, 'city' => '广州');
  $encoded_json = json_encode($arr); 

// 返回结果为json:
{"name":"shenghao","age":3,"city":"广州"}
?>
原文出处:
版权声明:本文来源地址若非本站均为转载,若侵害到您的权利,请及时联系我们,我们会在第一时间进行处理。