从JSON循环数据并放入codeigniter中的另一个函数

时间:2016-05-20 09:06:36

标签: php json codeigniter

我想从json循环数据并将结果放入控制器中的另一个函数。

获取json格式的函数

public function getApiSuperSpring() {
    //  Initiate curl
    $service_url="http://obd.id-clouds.net/raja_engine/GPS2.php?CMD=GETVEHICLE&APIKEY=1234&DOMAIN=@1111";
    $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    //execute the session
    $curl_response = curl_exec($curl);
    //finish off the session
    curl_close($curl);

    $decoded = json_decode($curl_response,TRUE);

    $arr = $decoded['VEHICLE']['DATA']; 

    $encoded = json_encode($arr, JSON_NUMERIC_CHECK);

    $decoded_again = json_decode($encoded, TRUE);


    $data = array();
    foreach($decoded_again as $item) { 
         array_push($data, $item['LATITUDE'].','.$item['LONGITUDE']);
    }

    return $data;
}

上述函数的JSON结果。

array(27) { 
    [0]=> string(18) "-6.261616,106.8098" 
    [1]=> string(17) "-6.26162,106.8101" 
    [2]=> string(18) "-6.140062,106.8314" 
    [3]=> string(18) "-6.175796,106.8762" 
    [4]=> string(18) "-6.257237,106.8226"
    [5]=> string(18) "-6.324639,106.8879" 
    [6]=> string(18) "-6.256825,106.8229" 
    [7]=> string(18) "-6.139688,106.8313" 
    [8]=> string(18) "-6.221396,106.9848" 
    [9]=> string(18) "-6.566057,106.7637"
    [10]=> string(18) "-6.180055,106.8224" 
    [11]=> string(18) "-6.220346,106.9223" 
    [12]=> string(18) "-6.233933,106.8254" 
    [13]=> string(18) "-6.369154,106.8271" 
    [14]=> string(18) "-6.210821,106.8166"
    [15]=> string(18) "-6.224951,106.8282" 
    [16]=> string(17) "-6.219806,106.813" 
    [17]=> string(17) "-6.243915,106.891" 
    [18]=> string(17) "-6.20253,106.8249" 
    [19]=> string(18) "-6.398064,106.8533"
    [20]=> string(17) "-6.39802,106.8537" 
    [21]=> string(18) "-6.228237,106.8252" 
    [22]=> string(18) "-6.150562,106.7151"
    [23]=> string(18) "-6.142435,106.7061" 
    [24]=> string(18) "-6.169276,106.9115"
    [25]=> string(18) "-6.193656,106.8788" 
    [26]=> string(18) "-6.345975,106.8228" }

我想要的结果如下

Coordinate 1 : -6.261616,106.8098
Coordinate 2 : -6.26162,106.8101 
Coordinate 3 : -6.140062,106.8314
..
Coordinate 26 : 6.345975,106.8228

如何将json结果传递给上面的格式。

2 个答案:

答案 0 :(得分:0)

制作你的自定义助手并将getApiSuperSpring放入其中并在你想要的任何控制器中调用这个助手我认为它不是带有codeignitor的大东西

答案 1 :(得分:0)

我自己也有线索。

var chart = c3.generate({
  bindto: '#chart',
  size: {
    height: 500,
   }
   ....
  onresized: function() {//callback called on resize of a chart
    resize()
  }
});