2016-12-31 5 views
0

私は実際にどのようにオブジェクトの配列を変換するのか分からない。私は必要なものPHP - オブジェクトに配列を設定

は私の配列は、このオブジェクトのように変形できます:

var locations= [ 
     {lat: -22.9, lng: -43.23}, 
     {lat: 23.03, lng: 113.12}, 
     {lat: 23.12, lng: 113.25}, 
     {lat: -7.24917, lng: 112.75083}, 
     {lat: -6.323116, lng: 106.870941}, 
     {lat: 40.69, lng: -73.99}, 
     {lat: 40.74, lng: -73.94} 
    ]; 

私はAJAXを使用してデータを取得する:

$(function() { 
     $.ajax({ 
      url   : '<?= base_url('admin/getCustomerLatLong'); ?>', 
      method  : 'GET', 
      // dataType : 'JSON', 
      success  : function(data) { 
       console.log(data); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       alert('Error while getting the data. Call the developer!'); 
      } 
     }); 
    }); 

と私はCodeIgniterのを使用しています、ここに私の中getCustomerLatLong関数でありますコントローラ:

public function getCustomerLatLong() { 
    $data = $this->M_customer->getAllCustomers()->result(); 
    $locations = array(); 

    foreach ($data as $location) : 
     $locations['lat'] = $location->latKota; 
     $locations['lng'] = $location->lngKota; 
     echo json_encode($locations); 
    endforeach; 
} 

私には、 ksを事前に入力します。

答えて

3

ループ内でechoを使用しているという事実に問題があると思います。

は次のような何かにあなたのコントローラーを変更してみてください:

public function getCustomerLatLong() 
{ 
    $data = $this->M_customer->getAllCustomers()->result(); 
    $locations = []; 

    foreach ($data as $location) { 

     $locations[] = [ 
      'lat' => $location->latKota, 
      'lng' => $location->lngKota, 
     ]; 
    } 

    echo json_encode($locations); 

} 

はまた、このことができますdataType : 'JSON',

希望のコメントを解除する必要があるかもしれません!

+0

あなたはGoogleマップのマーカークラスターを使ったことがありますか?私はそれらの場所に基づいてすべてのマーカーを設定する際に問題があります。私はいろいろ試してみましたが、マーカーは表示されず、コンソールログにもエラーは表示されませんでした。 –

+0

私は長い間それらを使っていませんが、別の質問を開いた場合、誰かが助けることができるはずです:) –

関連する問題