while ( $aSong = mysql_fetch_array($sql_get_files) ) {
//Creates an array of $thisSong[ID], $thisSong[Title], etc. substr removes the first 4 letters of the KEY
foreach ( $aSong as $sKey => $sValue ) $thisSong[substr($sKey,4)] = stripslashes($sValue);
//Ditch $thisSong for the prev value
$aSong = $thisSong; unset($thisSong);
$mp3r = '';
$tmp.= '{"name":"'.$aSong['Artist'].' '.$aSong['Title'].'","mp3":"http://domain.com/uploads/audio/'.$aSong['ID'].'.mp3"},';
}
echo rtrim($tmp,',');
Вы также можете просто заполнить массив:
$tmp_arr = array();
while ( $aSong = mysql_fetch_array($sql_get_files) ) {
//Creates an array of $thisSong[ID], $thisSong[Title], etc. substr removes the first 4 letters of the KEY
foreach ( $aSong as $sKey => $sValue ) $thisSong[substr($sKey,4)] = stripslashes($sValue);
//Ditch $thisSong for the prev value
$aSong = $thisSong; unset($thisSong);
$mp3r = '';
$tmp_arr[] = '{"name":"'.$aSong['Artist'].' '.$aSong['Title'].'","mp3":"http://domain.com/uploads/audio/'.$aSong['ID'].'.mp3"}';
}
echo implode(',',$tmp_arr);
Вам также нужно заключить в кавычки ваши ключи JSON, чтобы {mp3:"http...mp3"}
стало {"mp3":"http...mp3"}
. Если вы пытаетесь вывести JSON в браузер для AJAX или чего-то еще, вам понадобится что-то вроде этого:
echo '{"0":[' . implode(',',$tmp_arr) . ']}';
Как только вы получите все свои выходные данные, перейдите на JSONlint.com и подтвердите его.