0

code:

$prop_map = DB::table('jk_property_map_jpm')->select('jpm_location','jpm_longitude','jpm_latitude')->where('jpm_location','like','%'.$par2.'%')->get();
foreach($prop_map as $k)
{
    $array = array(
                    $k->jpm_location,
                    $k->jpm_latitude,
                    $k->jpm_longitude
                    );
    $data['map'] = json_encode($array);
    echo $data['map'];
}

API:

[
  "California Frazier Park 93222 15501 Nesthorn Way",
  "34.852633",
  "-119.149963"
][
  "Oakhurst,Gold Country,California,United States",
  "37.3392607",
  "-119.7114921"
][
  "Club de Golf Fonatur, San Jos\u00c3\u00a9 del Cabo, Baja California Sur, Mexico",
  "23.0522956",
  "-109.6987974"
]

In this code I am simply used json_encode to create an API and here I am getting unexpected output as I mention above. I want data as mention below:

[   
    [
      "California Frazier Park 93222 15501 Nesthorn Way",
      "34.852633",
      "-119.149963"
    ],
    [
      "Oakhurst,Gold Country,California,United States",
      "37.3392607",
      "-119.7114921"
    ],
    [
      "Club de Golf Fonatur, San Jos\u00c3\u00a9 del Cabo, Baja California Sur, Mexico",
      "23.0522956",
      "-109.6987974"
    ]
]

So, How can I create like this? Please help me.

Thank You

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
omkara
  • 974
  • 5
  • 24
  • 50

2 Answers2

1

You can use array_values to turn your associative array into a numeric array.

Change,

$data['map'] = json_encode($v);

To,

$data['map'] = json_encode(array_values($v));
Script47
  • 14,230
  • 4
  • 45
  • 66
1

Use laravel collection mapping function to format and return your results instead of iterating via loop and echoing out

$prop_map = DB::table('jk_property_map_jpm')->select('jpm_location','jpm_longitude','jpm_latitude')->where('jpm_location','like','%'.$par2.'%')->get();
$prop_map = $prop_map->map(function ($k) {
        return [
            $k->jpm_location,
            $k->jpm_latitude,
            $k->jpm_longitude, // viva trailing commas
            ];
    });
return $prop_map; // laravel automatically serializes arrays to JSON
Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
  • Thank you @Caddy DZ. I had a problem man i.e. I put `$prop_map` in `$data` like this `$data['locations'] = $prop_map` in my view file like `{{ $location }}` then it show `"` instead of double quots ` " ` So, How can I fix them? – omkara Aug 28 '19 at 10:09
  • Tha's because Blade echo directive `{{ }}` uses `html_escape` so you would have to trim the quotes in your php code before it reaches the view i.e `trim($location, '"');` – Salim Djerbouh Aug 28 '19 at 10:14
  • In case of jquery what will I do @Caddy DZ – omkara Aug 28 '19 at 10:20
  • In plain JS `location.replace(/\"/g, "")` – Salim Djerbouh Aug 28 '19 at 10:21
  • look I am fetching like `var data = {{ $location }}` and I am trying to replace `"` with `"` but nothing happen – omkara Aug 28 '19 at 10:25
  • @omkara please refer to this SO answer for a way to decode HTML special characters back to text in Javascript https://stackoverflow.com/a/4339083/5581565 – Salim Djerbouh Aug 28 '19 at 10:34
  • I had solved @Caddy DZ using {! $location !}. Thanks man for your kindness. – omkara Aug 28 '19 at 11:02
  • @omkara bad idea! If $location is supplied by your application users and you'd display it without sanitizing then you're vulnerable to XSS attacks – Salim Djerbouh Aug 28 '19 at 11:05