0

Im trying to do a foreach loop to get all the value in an array & another loop for addresses.

My code:

$json = json_decode($response, true);
foreach($json["result"]["vout"]["value"] as $a)
{
    echo "<br> $a";
}
{
    "result": {
        "vout": [
            {
                "value": 0.09998700,
                "n": 0,
                "scriptPubKey": {
                    "addresses": [
                        "16Tjr2hz67mQW1YHpSLnKKDoXi4na6D66W"
                    ]
                }
            },
            {
                "value": 1.63707231,
                "n": 1,
                "scriptPubKey": {
                    "addresses": [
                        "1LSnED1PUpssw9JAa2PPsypmjKrnMfz6u3"
                    ]
                }
            },
            {
                "value": 5.00000000,
                "n": 2,
                "scriptPubKey": {
                    "addresses": [
                        "17BkwbuiiDqMQGqBomZeR2XUmKzhv8EwDg"
                    ]
                }
            }
        ],
        "confirmations": 2885,
        "time": 1620396315,
        "blocktime": 1620396315
    },
    "error": null,
    "id": "curltest"
}

Error:

Warning: Undefined array key "value" in C:\xampp\htdocs\test.php on line 27
Warning: foreach() argument must be of type array|object, null given in C:\xampp\htdocs\test.php on line 27

Small information: $response is a json object i get from a curl $json is the json formatted in the standard php array structure

1 Answers1

2

There is some issue with your code as $json["result"]["vout"]["value"] does not exist.

So, you need to loop through over $json["result"]["vout"]

as below:

foreach($json["result"]["vout"] as $a)
{
    $value = $a['value'];
    echo "<br> $value";
}
dev_mustafa
  • 1,042
  • 1
  • 4
  • 14