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>';
                }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douzhong3038 2016-05-20 09:39

    make your custom helper and put getApiSuperSpring this function in it and call this helper in any controller you want i think its not big stuff with codeignitor

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题