首页 > 解决方案 > 转换 json 中的特殊字符串答案或获取数据 PHP 的访问权限

问题描述

我有这个:-

$str = 'hello';

$url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=fr&dt=t&q=" . $str;

$R=file_get_contents($url);

print($R);

我得到:-

 [[["Bonjour","hello",null,null,1] ] ,null,"en",null,null,null,1.0,[] ,[["en"] ,null,[1.0] ,["en"] ] ]

你能帮我访问'Bonjour''en'吗?

它是一个字符串,我正在尝试解码、解码等,但没有任何工作可以让事情变得正常。先感谢您

标签: phparraysjsonstringtransform

解决方案


您收到的数据是 json 格式,因此您应该对其进行解码。

$str = 'hello';
$url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=fr&dt=t&q=" . $str;

$result = file_get_contents($url);
$data = json_decode($result);

//print_r($data);

$bonjour = $data[0][0][0];
$en = $data[2];

echo $bonjour . ' ' . $en;

推荐阅读