如何将JSON文本转换为PHP关联数组 [英] How to converting JSON text to PHP associative array

查看:113
本文介绍了如何将JSON文本转换为PHP关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本文件(data.txt)中存储了以下JSON对象:

I have the following JSON Object stored in a text file(data.txt):

{"player":"black","time":"0","from":"2c","to":"3d"}

我使用php阅读的内容:

Which i read using php:

<?php
  $data = file_get_contents('data.txt');
?>

问题:是否有一种简单的方法可以将$data转换为PHP关联数组.我已经尝试过使用json_decode($data);了,但是那没用,有什么建议吗?

Question: Is there an easy way to convert $data to a PHP associative array. I have tried using json_decode($data); but that did not work, any suggestions?

推荐答案

$assocArray = json_decode($data, true);

第二个参数将结果设置为对象(false,默认值)或关联数组(true).

The second parameter set the result as an object(false, default) or an associative array(true).

这篇关于如何将JSON文本转换为PHP关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆