带有串联的PHP回音数组
你能帮我吗?我的代码有点问题带有串联的PHP回音数组,php,Php,你能帮我吗?我的代码有点问题 $json = '{"data":{"id":1,"first_name":"George","last_name":"Bluth","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"}}'; $response = json_decode($json, true); $output = ['data']['first_name']; echo $response
$json = '{"data":{"id":1,"first_name":"George","last_name":"Bluth","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"}}';
$response = json_decode($json, true);
$output = ['data']['first_name'];
echo $response.$output;
错误:行号=>echo$response上的数组到字符串转换。$output代码>
我怎样才能解决这个问题?我试了很多,但没有成功
谢谢。连接时必须使用
$json = '{"data":{"id":1,"first_name":"George","last_name":"Bluth","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"}}';
$response = json_decode($json, true);
$output = $response['data']['first_name'] ." " . $response['data']['last_name'];
echo $output;
die;
$response是一个数组,echo不知道如何显示数组
改用这个:
foreach($response['data'] as $value) {
echo $value.PHP_EOF;
}
您可以使用“first_name”或“last_name”代替['data']['first_name']:
$json = '{"data":{"id":1,"first_name":"George","last_name":"Bluth","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"}}';
$response = json_decode($json, true);
$output = 'first_name'; //your flexible variable.. first_name or last_name
echo $response['data'][$output];
如果数组键是动态的,则:
$key = 'first_name';
echo $response['data'][$key];
即使“数据”
键可以更改,那么:
$firstKey = 'data';
$secondKey = 'first_name';
echo $response[$firstKey][$secondKey];
这将对您有用:
$json='{“数据”:{“id”:1,“名字”:“乔治”,“姓氏”:“布鲁斯”,“阿凡达”:https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"}}';
$response=json_decode($json,true);
$output1=“数据”;
$output2=“名字”;
echo$response[$output1][$output2];
$output=$response['data']['first_name'];echo$输出代码>我知道,但脚本只是一个例子。因为$output
不是常量,这意味着有时候$output
可能会更改您对var_dump()的使用,因为您的响应是数组格式的,所以,这是对的,不是吗$根据“first_name”值的不同,每次输出都应该不同。不,我解释错了,对不起。有时,$output
可以是['data']['first\u name']
或['data']['last\u name']
。你明白了吗<代码>$output
可以更改。