Search code examples
phpjson

what's the best way of extracting a json value from a nested string?


in the code below, i would like to get the id and result code

{
      "data": {
      "transaction": {
                "id": "K3446tdgt08pe5QRgQ879",
                "status": "Success."
            }
        },
        "status": {
            "message": "Success.",
            "code": "200",
            "resultcode": "ESB000010",
            "success": true
        }
    }

Solution

  • I am expecting $json_string include the JSON result.

    <?php
    $array = json_decode($json_string, true);
    echo $array ["data"]["transaction"]["id"];
    ?>