With Treat a string by transforming it into an array

Asked

Viewed 135 times

0

I get a string from the bank that way:

POLYGON((-22.886145457836463 -43.118764097835, -22.88643210096987 -43.118324215556555, -22.886694032959 -43.117846782351904, -22.886763222456636 -43.11767512097496, -22.886782990878046 -43.117605383540564, -22.886886775043237 -43.11763757004875, -22.887133879879073 -43.11891966595787, -22.88630360584699 -43.11942392125267, -22.886115805063966 -43.118823106433325, -22.886145457836463 -43.118764097835))

I only need the coordinates between the parentheses:

-22.886145457836463 -43.118764097835, -22.88643210096987 -43.118324215556555, -22.886694032959 -43.117846782351904, -22.886763222456636 -43.11767512097496, -22.886782990878046 -43.117605383540564, -22.886886775043237 -43.11763757004875, -22.887133879879073 -43.11891966595787, -22.88630360584699 -43.11942392125267, -22.886115805063966 -43.118823106433325, -22.886145457836463 -43.118764097835

With Coordinates I need to create an array:

{lat: -22.886145457836463, lng: -43.118764097835},
{lat: -22.88643210096987, lng: -43.118324215556555},
{lat: -22.886694032959, lng: -43.117846782351904}

and etc...

How do I do?

  • Carlos I did not realize that your question is a duplicate, do not need to create multiple question for same solution.

1 answer

1


To extract this information, you need to work with 3 functions that are str_replace to remove what is unnecessary and then use explode creating a array through the spaces between the values and with Trim to remove the spaces initials and finals.

After that create a for at a pace 2 and do the new array from there having the keys lat and lng as requested in your question, minimal example:

<?php

$str = 'POLYGON((-22.886145457836463 -43.118764097835, -22.88643210096987
                -43.118324215556555, -22.886694032959 -43.117846782351904,
                -22.886763222456636 -43.11767512097496, -22.886782990878046
                -43.117605383540564, -22.886886775043237 -43.11763757004875,
                -22.887133879879073 -43.11891966595787, -22.88630360584699
                -43.11942392125267, -22.886115805063966 -43.118823106433325,
                -22.886145457836463 -43.118764097835))';

function array_final($str)
{
    $str = str_replace(['POLYGON','((','))'], [''], $str);
    $str = explode(" ", trim($str));
    $arrays = [];
    for($i = 0; $i < count($str); $i += 2)
    {
        $arrays[] = ['lat' => $str[$i], 'lng' => $str[$i+1]];
    }
    return $arrays;
}

var_dump(array_final($str));

ONLINE EXAMPLE

Observing: the Framework which is written in , will not solve all problems, especially the particular ones, often we need to brush code so that create certain solutions, which is in this case in specific.

References

  • Thank you very much my friend!!! worked with var_dump(array_final($str)); now how can I assign a variable to pass to view, can you just give me this help? Thanks a lot!!!!

  • As @Carlosalexandrerramos did not understand?

  • Good morning. Are we using var_dump to show the right results? When I try this for example: $result = array_final($str); does not work, but I need to assign this result to a understood variable?

  • The first result of the array brings: 0 => array:2 [▼&#xA; "lat" => "[{"area":"-22.886145457836463"&#xA; "lng" => "-43.118764097835,"&#xA; ] You can remove this area:

  • 1

    @Carlosalexe the example of the answer has a function created by me with the name of array_final, you know that text you need to turn into that array this method does this, var_dump is only to show but, the result of array_final is what you need!

  • Thank you very much I got, but have to take area of the array?

  • @Carlosalexall right, next, there are your questions unanswered, there are your questions with answers that you don’t interact with, and each question asked has some answers that solve the same problem, now you came and asked a sub question, take area of the array, I didn’t understand maybe fit a new question that relates this, but, there is one thing that the site still you do not understand, there is an exchange where the good answers can be punctuated and even accepted as a solution to your problem, and in your user has a lot in open, the staff begins not to answer your questions continues .

  • because you don’t do your part, start rethinking how you should behave and help not only yourself, but, everyone who understands here ... I ask: isn’t it better to open another question about the area removed from this array? how about ? @Carlosalexandrerramos

  • I’m sorry, Virgilio, but I’m not interacting in the right way because I didn’t know these details, but I think that whatever the main purpose is and we help each other, so that I will pay attention to this fact **I just want you to understand that it is not evil and only lack of knowledge **

  • @Carlosalexandrerramos do believe that it is lack of knowledge, so I am also warning you and wanting you to enjoy the best possible way, and help the community grow, about the area ask a question is how it works, each question asks a question ... a well contextualized question is what we need to help you ...

  • Thank you, Virgilio, thank you. the link of the new question is to remove the area also please: https://answall.com/questions/242789/tratamento-de-arrays/242816#242816

Show 6 more comments

Browser other questions tagged

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