知向前端
php写入和读取json文件demo
2015-11-8 Jon
1、php创建并写入json文件

<?php

  header('Content-type: text/json');

  $fruits = array (

      "fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"),

      "numbers" => array(1, 2, 3, 4, 5, 6),

      "holes"   => array("first", 5 => "second", "third")

  );

  // 创建 转化 写入

  file_put_contents('data.json',json_encode($fruits));

?>



2、php读取json文件

<?php

  // 读取json

  $json_string = file_get_contents('data.json');

  // 转化为数组

  $data = json_decode($json_string, true);

  // 输出

  var_dump($data);

?>
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容