-1

I have an array($coords) in php which contains some coordinates.

print_r($coords)

gives

Array ( [0] => 80.23338342111089 ,26.52748985468579 [1] => 80.21915925983853 ,26.51065783417543 [2] => 80.23036859334592 ,26.49785585262899 [3] => 80.24667293123426 ,26.51075259323626 [4] => 80.23338342111089 ,26.52748985468579 )

Then I used $string = implode(",",$coords); print_r($string);

It gave me

80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579

I passed this to Javascript like this-

    var Coordinates = [];
    Coordinates.push(<?php echo ($string); ?>);

now I want to stringify this so I used-

var JSON_Coordinates = JSON.stringify(Coordinates);

Now the value of JSON_Coordinates is-

[80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579]

But it should be-

  [[80.23338342111089 ,26.52748985468579],[80.21915925983853 ,26.51065783417543],[80.23036859334592 ,26.49785585262899],[80.24667293123426 ,26.51075259323626],[80.23338342111089 ,26.52748985468579]]

Someone please tell me where am i wrong?

Thanks.

4
  • 1
    stackoverflow.com/questions/7030229/… Commented Mar 22, 2018 at 18:46
  • Considering your code, I suppose $coords is more a string than an array. Commented Mar 22, 2018 at 18:50
  • 1
    Possible duplicate of storing coordinates in array in javascript Commented Mar 22, 2018 at 18:52
  • So you got varied answers... be sure to accept one and close out your question. Commented Mar 22, 2018 at 19:35

4 Answers 4

1

Use array_chunk to split the array to pairs

$arr = [80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579];
?>
var Coordinates = <?= json_encode(array_chunk($arr,2)) ?>;
// var Coordinates = [[80.233383421111,26.527489854686],[80.219159259839,26.510657834175],[80.230368593346,26.497855852629],[80.246672931234,26.510752593236],[80.233383421111,26.527489854686]];

demo

Sign up to request clarification or add additional context in comments.

2 Comments

how would I place coordinates inside []?
I have edited the question. Now it has complete details. Please have a look!
0

Would need to see how the array is being established in your PHP code. Are they fetched as pairs or all in one big array?

Pretty straightforward. Just has to do with structure of the array you are converting to JSON.

$foo = array(1, 2, 3, 4);
// [1,2,3,4]

$bar = array(array(1, 2), array(3, 4));
// [[1,2],[3,4]]

I'd clean up the data server side, so you just have to JSON encode it, and then pass that client side.

Comments

0

After you get the array of coordinates, you can loop over it to build the desired output.

An alternative is using a for-loop.

var coordinates = [80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579];

var newCoordinates = [];
for (var i = 0; i < coordinates.length; i++) {
  if (i % 2 !== 0) newCoordinates.push([coordinates[i - 1], coordinates[i]]);
}

console.log(newCoordinates);
.as-console-wrapper { max-height: 100% !important; top: 0; }

According to your initial array, you can loop, split and then build the nested arrays:

Initial input data: ["c1, c2", "c1, c2", ...]

This way, you don't need to call the function stringify.

var array = ["80.23338342111089 ,26.52748985468579", "80.21915925983853 ,26.51065783417543","80.23036859334592 ,26.49785585262899", "80.24667293123426 ,26.51075259323626","80.23338342111089 ,26.52748985468579" ];

var result = [];
for(var i = 0; i < array.length; i++) {
  var split = array[i].split(',');
  var lt = split[0].trim();
  var ln = split[1].trim();
  result.push([+lt.trim(), +ln.trim()])
}

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

5 Comments

This is giving me error -"coordinates.reduce is not a function"
this is original array($coords)- Array ( [0] => 80.23338342111089 ,26.52748985468579 [1] => 80.21915925983853 ,26.51065783417543 [2] => 80.23036859334592 ,26.49785585262899 [3] => 80.24667293123426 ,26.51075259323626 [4] => 80.23338342111089 ,26.52748985468579 ) Then I used $new = implode(",",$coords) So new one is 80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579
So, initially is an array of strings as follow: ["c1, c2", "c1, c2", ...]?
yes but then I tried to change it to use JSON.stringify
@butch I'm going to update the answer with an approach using that input data: ["c1, c2", "c1, c2", ...] so you won't need the stringify
0

You should write your JS logic like that way. At this moment you are doing nothing in your code that will create a desired 2d array.

You should loop through whole array and increment counter by 2 to get 2 elements at a time and create a new temporary array that will push in your original arrCoordinates.

Coordinates supply as array

var coordinates = [80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579];

var arrCoordinates =[];
for(var index=0; index< coordinates.length; index+=2){
    arrCoordinates.push([coordinates[index], coordinates[index+1]]);
}

console.log(arrCoordinates);

Coordinates supply as string

var coordinates =  "80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579";

var arrCoordinates =[];
var inputCoordinates = coordinates.split(",");
for(var index=0; index< inputCoordinates.length; index+=2){
    arrCoordinates.push([inputCoordinates[index].trim(), inputCoordinates[index+1].trim()]);
}

console.log(arrCoordinates);

20 Comments

I think you meant: ++index
sorry please let me know if anywhere I am wrong. I don't understand your meaning my friend.
thank you for your sharing a knowledge and explain me.
can you send your input array, before you do anything?
Butch your current array is quite different than question at present array so we should give you an answer according to your question.
|

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.