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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化