php 转json数据

<?php
/**
 * Created by PhpStorm.
 * User: Simtao
 * Date: 2018/5/3
 * Time: 23:19
 */
$data = array();
$data['time']=time();
$data['a'] = 'test';
$data['b'] = 'ccc';

if(file_exists('./test.json')){
    $toutiao = json_decode(file_get_contents('./test.json'),true);
    //if($toutiao['time']>@strtotime(date("Y-m-d"))){
    if(time()<=$toutiao['time']+60){
        foreach($data as $key=>$val){
                $toutiao = $val;
        }
        $data = $toutiao;
    }
}
// 写入文件
file_put_contents('./test.json', json_encode($data));
$json_string = file_get_contents('./test.json');

// 把JSON字符串转成PHP数组
$data = json_decode($json_string, true);


// 显示出来看看
var_dump($data);

猜你喜欢

转载自my.oschina.net/SimTao/blog/1806501