Amir Rachman Amir Rachman - 6 months ago 28
JSON Question

Loop data from JSON and put into another function in 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.

Answer

I've got clue by myself.

public function abc() {

    $result_array = $this->getApiSuperSpring();

    $marker = array();

            foreach($result_array as $item) { 
                echo 'Coordinate'. $item.'<br>';
            }
}
Comments