2017-03-20 13 views
1

array_merge()json_decode()を探し回っていました。json出力の配列をマージする

私は、コードを持っているが、それはファイルを介して検索し、検索にデータ情報(情報はJSON出力として返す)

ファイルの内容を引き戻すことをプッシュし、各ラインをループしました:

spotify:track:2SZy40PLDk3vFucXUGFCFA 
spotify:track:1ZQnV7ePl8yXoLjPfhWE5L 
spotify:track:4NbvIwYcwx8dNGYfUX2bKB 

出力こだま:私は、トラックと呼ばれる1つのJSONのthatsにすべてのデータをマージ{"tracks":[{"のようなものとヘクタールの内側にしたい

{"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の情報を確認してください。

マイコード:

$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

私は配列を作成し、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); 

私はそれにも名前を付けることができますか?

関連する問題