duandaotui5633 2016-05-20 09:06
浏览 85
已采纳

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

I want to loop the data from json and put the result into another function in controller.

function to get the json format

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 Result from function above.

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" }

What result that i want is like below

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

How to pass that json result into above format.

  • 写回答

2条回答 默认 最新

  • dozug64282 2016-05-23 04:47
    关注

    I've got clue by myself.

    public function abc() {
    
        $result_array = $this->getApiSuperSpring();
    
        $marker = array();
    
                foreach($result_array as $item) { 
                    echo 'Coordinate'. $item.'<br>';
                }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题