ホームページ >バックエンド開発 >PHPチュートリアル >phpの配列をjsに渡す問題の解決方法。
php の配列を js に渡す問題を解決します。
js は php ファイルを非同期的にリクエストし、配列を返します echo js_encode($list);
配列の出力は次のとおりです: function(data){alert(data) }
{" 1":{"id":54,"neme":"sdfa","0":"pic","1":"http://localhost/xin/index.php?app =home&mod=new&act=newlist "},"2":{"id":46,"neme":"sdfa","0":"pic","1":"http://localhost/xin/index .php?app=home&mod =welcome&act=newlist"},"3":{"id":56,"neme":"u5212","0":"pic","1":"http://localhost /xin/index.php ?app=home&mod=welcome&act=u611fu5230"},"4":{"id":58,"neme":"sdfa","0":"pic","1":"http ://localhost/xin /index.php?app=space&mod=welcome&act=u548c"},"5":{"id":87,"neme":"u7ba1u7406u5458","0":"pic","1 ":"http:///localhost/xin/index.php?app=home&mod=come&act=newlist"},"6":{"id":54,"neme":"sdfa","0":"pic ","1": "http://localhost/xin/index.php?app=home&mod=welcome&act=u8c01u7684"}}
しかし、for ループが出力されると、シートがほどけていることがわかります...コードは次のとおりです。
function (data){
alert(data);
alert(data.length);//ポップアップは 692
for(var i=0;i
}
ただし、ポップアップ配列の値はコピーできます...
次のように:
var data1={"1 ":{"id":54,"neme":"sdfa","0":"pic","1":"http: //localhost/xin/index.php?app=home&mod=new&act=newlist" },"2":{"id":46,"neme":"sdfa","0":"pic","1" :"http://localhost/xin/index.php?app=home&mod= welcome&act=newlist"},"3":{"id":56,"neme":"u5212","0":"pic" ,"1":"http://localhost/xin/index.php?app=home&mod=welcome&act=u611fu5230"},"4":{"id":58,"neme":"sdfa","0" :"pic","1":"http://localhost/xin/index.php?app=space&mod=welcome&act=u548c"},"5":{"id":87,"neme":"u7ba1u7406u5458" ,"0":"写真","1":"http://localhost/xin/index.php?app=home&mod=come&act=newlist"},"6":{"id":54,"neme" :"sdfa","0":"pic","1":" http://localhost/xin/index.php?app=home&mod=welcome&act=u8c01u7684"}}
.... ......................................................
とは続いてるの?
-----解決策---------
PHP の echo js_encode($list);
は js の文字列を取得するので、それを js オブジェクトに変換する必要があります。
最も簡単な方法は