Treatment of Arrays

Asked

Viewed 102 times

2

Guys I’m getting an array:

[{lat: "[{"area":"-22.88975203013098", lng: "-43.12695211119432,"},…]
0
:
{lat: "[{"area":"-22.88975203013098", lng: "-43.12695211119432,"}
1
:
{lat: "-22.88977179811704", lng: "-43.12685018725176,"}
2
:
{lat: "-22.88979650809557", lng: "-43.126624881694504,"}
3
:
{lat: "-22.890513095516", lng: "-43.12649077124376,"}
4
:
{lat: "-22.89054274732773", lng: "-43.126807271907516,"}
5
:
{lat: "-22.88975203013098", lng: "-43.12695211119432"}]"}

I need to take that part out:

"[{"area":"`

how do I do?

  • And where is "area" in that code you put ?

  • 1

    That’s a json, use the function json_decode() to manipulate him

1 answer

3


One option is to use regex. Based on the section you want to remove, you would use the method preg_replace to replace the desired value with a string empty. The pattern would be:

$pattern = '/\"\[{"area":"/s'; 

Behold functioning on the ideone.

See here the regex working.

And as a bonus, see below with Javascript.

str = '[{lat: "[{"area":"-22.88975203013098", lng: "-43.12695211119432,"},…] 0 : {lat: "[{"area":"-22.88975203013098", lng: "-43.12695211119432,"} 1 : {lat: "-22.88977179811704", lng: "-43.12685018725176,"} 2 : {lat: "-22.88979650809557", lng: "-43.126624881694504,"} 3 : {lat: "-22.890513095516", lng: "-43.12649077124376,"} 4 : {lat: "-22.89054274732773", lng: "-43.126807271907516,"} 5 : {lat: "-22.88975203013098", lng: "-43.12695211119432"}]"}';

console.log(str.replace(/\"\[{"area":"/g,'')); 

  • How can I use together with the function: function array_final($str)&#xA;{&#xA; $str = str_replace(['POLYGON','((','))'], [''], $str);&#xA; $str = explode(" ", trim($str));&#xA; $arrays = [];&#xA; for($i = 0; $i < count($str); $i += 2)&#xA; {&#xA; $arrays[] = ['lat' => $str[$i], 'lng' => $str[$i+1]];&#xA; }&#xA; return $arrays;&#xA; &#xA;}&#xA;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.