2017-10-18 5 views
0

を使用して、配列の配列を取得するにはどのように、私は、クエリのmysqlから配列に値の配列を取得したいです。しかし、私はコンソールでチェック、私は配列のオブジェクトを取得します。私はサンプルのデータと私のクエリmysqlからのデータを使ってサンプルコードを書いていきます。私のjqueryのjqueryので

var locationsSample = [ 
       [-6.175656, 106.812630,'ONSHORE',1,'id1'], 
       [-6.192848, 106.822027,'OFFSHORE',1,'id2'], 
       [-6.164234, 106.328973,'FABRICATION SHOP',1,'id3'], 
       [-0.594009, 100.735315,'SUMATERA',0,'id4'], 
       [5.168138, 97.124416,'SUMATERA ACEH',1,'id5'] 
     ]; 
console.log(locationsSample); 
from console => Array [ Array[5], Array[5], Array[5], Array[5], Array[5] ] this is a true. 

、これが私のPHPコードです:

ファイル名:jsonarray.php

$qry="SELECT latitude,longitude,project_name,status,id_tbl_project_map FROM tbl_project_map"; 
$res = mysql_query($qry) or die('Query failed: ' . mysql_error()); 
$rows= array(); 
while($data=mysql_fetch_assoc($res)) 
{ 
    $rows[]=$data; 
} 
print json_encode($rows); 

とjqueryのファイルjsonarray.php呼び出すために:ありがとう、

locations = new Array(); 
     $.ajax({ 
      url:"jsonarray.php?id=2", 
      type:"POST", 
      dataType:"json", 
      success:function(retqry){ 
       locations = retqry; 
       console.log(locations); 
from console => Array [ Object, Object, Object, Object, Object ] this is a false. why i'm get a Object not Array[5] from example code in top 
      } 
     }); 

が助けてください君は。

+0

'Object'項目に正しいデータがありますか?あなたは 'mysql_fetch_assoc'とJSONをエンコーディングしているので、それをObjectとして解釈します。 (* mysqlは非推奨でセキュリティ上の欠陥があるので注意してください。*) –

+0

コンソールでオブジェクトをクリックしてから、有効なデータまたはデータを表示してください。 –

+0

彼らは正しいので、ここで間違ったことはありません。 PHPの連想配列項目がJSONのオブジェクトとして解釈されるため、なぜ最後のコメントの 'Object'であるのか説明しました。 –

答えて

0

は、あなたがあなたの必要性に従って、コードを変更する必要があります。このコードはテストされていませんのでご注意ください

locations = new Array(); 
$.ajax({ 
    url:"jsonarray.php?id=2", 
    type:"POST", 
    dataType:"json", 
    success:function(retqry){ 
     // locations = retqry; 
     // console.log(locations); 
     var Data = []; 
     $.each(retqry, function(your_key, your_val){ 
      Data.push({ 
       your_key:your_val, 
      }); 
     }); 

     locations = Data; 
    } 
}); 

、あなたの条件をアーカイブするためにこの方法を試してみてください。これはあなたのために働くことを望みます!

ご挨拶!

+0

ありがとうございますが、私の場合はあなたのコードはまだ動作しません。 –