通过使用下面的PHP输出下面的值。但是因为我无法控制API输出,所以我想尝试将数据添加到另一个输出中,例如{"id":0,"name":"Begin","description":"this is my description","url":"http://domain.com"}
有人可以帮忙吗?
PHP
<?php
$jsonurl = "/api/styles";
$json = file_get_contents($jsonurl);
echo $json;
?>
OUTPUTS
[{"id":1,"name":"Pale","description":"this is my description","url":"http://domain.com"},{"id":2,"name":"Dawn","description":"this is my description","url":"http://domain.com"}]
答案 0 :(得分:1)
这是你的想法吗?
<?php
$jsonurl = "/api/styles";
$json = file_get_contents($jsonurl);
$json = '[{"id":0,"name":"Begin","description":"this is my description","url":"http://domain.com"},' . substr($json, 1);
echo $json;
?>
输出:
[{"id":0,"name":"Begin","description":"this is my description","url":"http://domain.com"},{"id":1,"name":"Pale","description":"this is my description","url":"http://domain.com"},{"id":2,"name":"Dawn","description":"this is my description","url":"http://domain.com"}]
答案 1 :(得分:1)
$jsonurl = "/api/styles";
$json = file_get_contents($jsonurl);
$begin = '{"id":0,"name":"Begin","description":"this is my description","url":"http://domain.com"}';
$data = json_decode($json);
array_unshift($data, json_decode($begin));
echo json_encode($data);
答案 2 :(得分:0)
如果它真的是json,解析它(decode_json())并打印它,无论如何。
答案 3 :(得分:0)
我不确定你想要“prepend”的是什么,但是你可以把它变成一个PHP数组,反过来然后将新元素推到数组上。
$json = file_get_contents($jsonurl);
$array = json_decode($json, true);
$reversed = array_reverse( $array );
$reversed[] = ['id' => 0, 'name' => 'Something];
将其反转并编码。