我一直在四处寻找并遇到array_merge()
和json_decode()
。
我有代码,它通过一个文件查看并循环遍历每一行,然后将该数据推送到搜索中以撤回信息(信息返回为json输出。)
文件内容:
spotify:track:2SZy40PLDk3vFucXUGFCFA
spotify:track:1ZQnV7ePl8yXoLjPfhWE5L
spotify:track:4NbvIwYcwx8dNGYfUX2bKB
回声输出:
{"type":"track","artist":"Jax Jones, Raye","title":"You Don't Know Me","album":"You Don't Know Me","duration":214000,"offset":0,"available":true,"popularity":88} {"type":"track","artist":"MK, Becky Hill","title":"Piece of Me","album":"Piece of Me","duration":189000,"offset":0,"available":true,"popularity":65} {"type":"track","artist":"Wankelmut, Emma Louise","title":"My Head Is A Jungle - MK Remix / Radio Edit","album":"My Head Is A Jungle (MK Remix / Radio Edit)","duration":205000,"offset":0,"available":true,"popularity":62}
我想将所有数据合并到一个称为轨道的json中,类似{"tracks":[{"
,内部有json信息。
我的代码:
$contents = '';
$dataarray = file('/location/'.$_GET['playlist'].''); //Push file data into array
$finallist = '';
//Grab Track Info
//echo count($dataarray);
foreach ($dataarray as $line_num => $line) //Loop Through Data
{
$line = str_replace("\n", "", $line); //Replace new line on string
$contents = searchCommand($connect, 'uinfo '.$line); //Returns Json for that single track
if (stripos($contents, '"error":"invalid argument (should be a Spotify URI)"') == FALSE && stripos($contents, '"error": "invalid command"') == FALSE) //If we found tracks
{
echo $contents;
}
else
{
echo "Fail";
}
}
答案 0 :(得分:0)
我创建了一个数组并将其转换为json,因此我可以通过JS
阅读它。
foreach ($dataarray as $line_num => $line) //Loop Through Data
{
$line = str_replace("\n", "", $line); //Replace new line on string
$contents = searchSpopCommand($spop, 'uinfo '.$line); //Returns Json for that single track
if (stripos($contents, '"error":"invalid argument (should be a Spotify URI)"') == FALSE && stripos($contents, '"error": "invalid command"') == FALSE) //If we found tracks
{
$finallist .= $contents;
}
else
{
echo "Fail";
}
}
$array = explode("\n", $finallist);
array_pop($array);
echo json_encode($array);
如果我能说出一个名字,那将是理想的!