Search code examples
phpstringobject

How to remove strings and colons before object in PHP?


I am looking to remove strings and colon symbols prior to an object so that I can search the object for its entities.

For example, I have:

{ "result": [ { "face_id": "b668c61ad349ea928c75ba46338008af", "landmark": { "contour_chin": { "x": 55.448237, "y": 35.152167 }, "contour_left1": { "x": 40.641011, "y": 26.241833 }, "contour_left2": { "x": 40.791324, "y": 27.7615 }

and I would like to have:

{55.448237, 35.15216},{40.641011,26.241833 },{40.791324,27.7615 }

If there's no easy way to remove the strings, can you recommend an easy way to search for the corresponding number. So.. I search for "contour_left2" and get {40.791324,27.7615 } returned.

Thank you.


Solution

  • So as it was suggested by @JimL you are dealing with a json string. If you decode it:

    $data = @json_decode($string);
    

    Then $data will be an object, and you will be able to access it's properties:

    $contour_left2 = $data->result[0]->landmark->contour_left2;
    // $contour_left2->x, $contour_left2->y
    

    (Of course you should do some checking first to make sure $data->result is not empty, etc...)

    I'm not sure if you absolutely need the coordinates as a simple tuple, but you could then build a small array:

    $tuple = array($contour_left2->x, $contour_left2->y);
    

    Hope this helps!