【发布时间】:2016-02-20 20:16:53
【问题描述】:
我是程序员的初学者,我正在学习。 如果我的问题太糟糕,我很抱歉。
我想根据api内容在php中创建变量,例如:
此内容来自此网址:http://example.com/api
{"name":"John","age":"20","genre":"male","language":[{"id":"22","name":"english"} ,{"id":"23","name":"french"}]}
<?php
$content = file_get_contents("http://example.com/api");
$content = str_replace('"', "", $content);
$content = str_replace(":", "=", $content);
$content = str_replace(",", "&", $content);
parse_str($content);
echo $name; //John
echo $age; //20
echo $genre; //male
echo $language //[{id <======== here is my problem
?>
我的问题是当我得到一个像“语言”这样的数组时,如何解决它?
感谢您的帮助。
【问题讨论】:
-
你的API内容是JSON字符串:使用
json_decode($content)直接转换成php变量/对象。 -
@fusion3k 谢谢,我必须了解更多.. :)