0

I am trying to add multiple markers into a google map and i have found a way to do that here. Now I have a json array response from the server as shown below.

 // function to get user names and addresses
public function getUserAddresses(Request $request)
{
    $users = User::where('address', '!=', null)->select('name', 'address')->get();  //this is a laravel query

    $userData = [];

    foreach ($users as $user) {
        $userData[$user->name] = $user->address;
    }
    return $userData;
}

This code above is what gives me the response below.

{
  "plumber1": "-1.2523238641713191,36.87899683074249",
  "plumber2": "-1.2192245641713191,36.87899687428849",
  "allan plumber": "-1.2192238641713191,36.87899683068849"
}

but for me to use this data it must be in these format as shown below in javascript.

[
   ["plumber1", -1.2523238641713191,36.87899683074249],
   ["plumber2", -1.2192245641713191,36.87899687428849],
   ["allan plumber", -1.2192238641713191,36.87899683068849]
];
Bhavin
  • 3
  • 2

2 Answers2

0

Try json_decode

ini_set( 'precision', 17 );

$jsonToArray = json_decode($json, JSON_NUMERIC_CHECK);
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

You can use this php code

$str = '{
  "plumber1": "-1.2523238641713191,36.87899683074249",
  "plumber2": "-1.2192245641713191,36.87899687428849",
  "allan plumber": "-1.2192238641713191,36.87899683068849"
}';

$array =json_decode($str);
$new_array = [];
foreach ($array as $key => $value) {
  $coordinates = explode(',',$value);
  $coordinate1 = (float) $coordinates[0];
  $coordinate2 = (float) $coordinates[1];
  $new_array[] = array($key,$coordinate1,$coordinate2);
}
print_r($new_array);

OUTPUT

        Array
(
    [0] => Array
        (
            [0] => plumber1
            [1] => -1.25232386
            [2] => 36.878996830742
        )

    [1] => Array
        (
            [0] => plumber2
            [1] => -1.2192245641713
            [2] => 36.878996874288
        )

    [2] => Array
        (
            [0] => allan plumber
            [1] => -1.2192238641713
            [2] => 36.878996830688
        )

)

You can also check the demo here

Here is Javascript version

    <script type="text/javascript">
var str = `{
  "plumber1": "-1.2523238641713191,36.87899683074249",
  "plumber2": "-1.2192245641713191,36.87899687428849",
  "allan plumber": "-1.2192238641713191,36.87899683068849"
}`;
var obj = JSON.parse(str);
var array = [];
var counter =0;
for (var key in obj) {
  var myarr =  obj[key].split(",");
    array[counter] = [key, parseFloat(myarr[0]), parseFloat(myarr[1])];
    counter++
}
console.log(array);
</script>
Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
  • Thanks, Now the JSON array that i want to remove the Quotations from is a response from a get request..Meaning it is dynamic and not a string.. so from your answer. here -> $array =json_decode($str); i have to put the response direct..not a string.. – Bhavin Apr 20 '19 at 18:54
  • and i was requesting to change the response in javascript.. – Bhavin Apr 20 '19 at 18:55
  • I have updated my answer. Now it will give you floating numbers instead of strings – Zain Farooq Apr 20 '19 at 18:57
  • I have uploaded both versions like javascript and php. You can now enjoy it... :) – Zain Farooq Apr 20 '19 at 19:18
  • 1
    Omg!!! Thank you so much... @Zain Farooq – Bhavin Apr 20 '19 at 19:43