1

i have an array data and i want to push that data like this :
new LatLng([ 'lat' => -8.3901371, 'lng' => 115.211049 ]),new LatLng([ 'lat' => -8.3901953, 'lng' => 115.2110649 ]),new LatLng([ 'lat' => -8.3902752, 'lng' => 115.2110648 ]),

then i write code like this :

$paths=[];
foreach ($data as $value){
    $koord = "new LatLng([
        'lat' => $value->wilayah_lat,
        'lng' => $value->wilayah_lng
    ]),";
    array_push($paths, $koord);      
 }

then, i want to access $paths using print_r($paths), i always get data like this :
Array ( [0] => new LatLng([ 'lat' => -8.3901371, 'lng' => 115.211049 ]), [1] => new LatLng([ 'lat' => -8.3901953, 'lng' => 115.2110649 ]), [2] => new LatLng([ 'lat' => -8.3902752, 'lng' => 115.2110648 ]),

my question is, how do i access that data array on variable $paths look like that i want above ?

6
  • what is the error you are getting ?? working fine for me. Commented Apr 18, 2015 at 6:37
  • so you'd like it to be as it will look like a string? Commented Apr 18, 2015 at 6:38
  • @ris i want to access array $paths as string, like this new LatLng([ 'lat' => -8.3901371, 'lng' => 115.211049 ]),new LatLng([ 'lat' => -8.3901953, 'lng' => 115.2110649 ]),new LatLng([ 'lat' => -8.3902752, 'lng' => 115.2110648 ]), Commented Apr 18, 2015 at 6:41
  • after array_push you can use impload function . Commented Apr 18, 2015 at 6:43
  • If you want to store data as one complete string then why do you need the array functionality? Commented Apr 18, 2015 at 8:08

4 Answers 4

1

Array store data with keys. SO your array look like Array ([0] => 'value ,...)

If you want plain string use this code

implode(',',$array);
Sign up to request clarification or add additional context in comments.

Comments

1

you can use implode for this

 $paths=[];
    foreach ($data as $value){
        $koord = "new LatLng([
            'lat' => $value->wilayah_lat,
            'lng' => $value->wilayah_lng
        ])";                                    // remove the , from here .
        array_push($paths, $koord);      
     }

    echo implode(',',$paths);

1 Comment

impload should be implode
0

Its still in array form, thats why when you print_r it, it still has its numeric index like the one shown. Either you implode the final array:

foreach ($data as $value){
    $koord = "new LatLng([
        'lat' => $value->wilayah_lat,
        'lng' => $value->wilayah_lng
    ])";
    $paths[] = $koord;      
}

$paths = implode(', ', $paths);
echo $paths;

Or make/initialize $paths as strings, then continually append/concatenate the rest of the strings in the loop:

$paths = '';
foreach ($data as $value){
    $paths .= "new LatLng([
        'lat' => $value->wilayah_lat,
        'lng' => $value->wilayah_lng
    ]),";     
}

$paths = rtrim($paths, ','); // remove excess/last comma
echo $paths;

Comments

-2

To get effect like you want, retrieve data from array like this:

foreach($paths as $path){

    echo $path; 

}

$paths is the array from which you want to retrieve data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.