Php 合并json输出的数组

Php 合并json输出的数组,php,json,Php,Json,我环顾四周,发现了array\u merge()和json\u decode() 我有一段代码,它浏览一个文件并循环每一行,然后将数据推送到搜索中以提取信息(信息作为json输出返回) 文件内容: spotify:track:2SZy40PLDk3vFucXUGFCFA spotify:track:1ZQnV7ePl8yXoLjPfhWE5L spotify:track:4NbvIwYcwx8dNGYfUX2bKB 回声输出: {"type":"track","artist":"Jax Jon

我环顾四周,发现了
array\u merge()
json\u 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} 
我想将所有这些数据合并到一个名为tracks的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";
    }
}

我创建了一个数组并将其转换为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);

如果我能给它起个名字,那就太理想了。

我创建了一个数组并将其转换为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);
如果我能给它起个名字,那就太理想了