Home > Software engineering >  Parsing JSON object foreach
Parsing JSON object foreach

Time:08-12

I can't access my object in json for all. I can access for one by one but i want to print foreach object. I'm trying to get 'txID' for each array.But it's printing nothing. I've already tired the given solution on Parsing JSON array with PHP foreach But it didn't work.

Here is my json

                            {
   "data":[
      {
         "ret":[
            {
               "contractRet":"SUCCESS",
               "fee":0
            }
         ],
         "signature":[
            "59ebd5e92c05638d39ddea273cab2b6caf6fcd2e32c790699fa620ab5dcd70496bcd9b73a66c5299e1375b3ea87676dfd0efb7c8914d2f7f9696c8e9ba0c5fe000"
         ],
         "txID":"b7db5efabb829c19ecffb642e726212e7c06ad5b44bd2c0324a525ed4991923a",
         "net_usage":329,
         "raw_data_hex":"0a026b1222087c93c9355444acf940b1f08fc1e72f522d53707265616420756e636f6e646974696f6e616c206c6f76652c206d616b65206120686170707920776f726c645a76080212720a32747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e736665724173736574436f6e7472616374123c0a07313030343033311215419a098706852100c5beeb5054a36a8bdd808a99781a15419b3c8777809cefab5c6116af884af6b2d674be7220c8fb830470d19b8cc1e72f",
         "net_fee":0,
         "energy_usage":0,
         "blockNumber":37382949,
         "block_timestamp":1642693071000,
         "energy_fee":0,
         "energy_usage_total":0,
         "raw_data":{
            "data":"53707265616420756e636f6e646974696f6e616c206c6f76652c206d616b65206120686170707920776f726c64",
            "contract":[
               {
                  "parameter":{
                     "value":{
                        "amount":8453576,
                        "asset_name":"1004031",
                        "owner_address":"419a098706852100c5beeb5054a36a8bdd808a9978",
                        "to_address":"419b3c8777809cefab5c6116af884af6b2d674be72"
                     },
                     "type_url":"type.googleapis.com/protocol.TransferAssetContract"
                  },
                  "type":"TransferAssetContract"
               }
            ],
            "ref_block_bytes":"6b12",
            "ref_block_hash":"7c93c9355444acf9",
            "expiration":1642693130289,
            "timestamp":1642693070289
         },
         "internal_transactions":[
            
         ]
      },
      {
         "ret":[
            {
               "contractRet":"SUCCESS",
               "fee":268000
            }
         ],
         "signature":[
            "c2e6f20a7786bcbb1867c0c6d8beaccd311b5a5c962a09b5f725b4a04f28e69b663cd87575b96f882c1180cd322e34259ee75849686e9b6348a29809b4aa7d7b01"
         ],
         "txID":"c3c9ac55378a212f81f3b242b836fcd2a2376c43f8449da8a4787895dcd6d637",
         "net_usage":0,
         "raw_data_hex":"0a0231952208577691a0d4aab94b40e0f2d7e4dd2f5a68080112640a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412330a15410bc6c882e8cc55c70fd4abae47ad504c48d6e3951215419b3c8777809cefab5c6116af884af6b2d674be721880dac40970cfb0d4e4dd2f",
         "net_fee":268000,
         "energy_usage":0,
         "blockNumber":36516246,
         "block_timestamp":1640083299000,
         "energy_fee":0,
         "energy_usage_total":0,
         "raw_data":{
            "contract":[
               {
                  "parameter":{
                     "value":{
                        "amount":20000000,
                        "owner_address":"410bc6c882e8cc55c70fd4abae47ad504c48d6e395",
                        "to_address":"419b3c8777809cefab5c6116af884af6b2d674be72"
                     },
                     "type_url":"type.googleapis.com/protocol.TransferContract"
                  },
                  "type":"TransferContract"
               }
            ],
            "ref_block_bytes":"3195",
            "ref_block_hash":"577691a0d4aab94b",
            "expiration":1640083356000,
            "timestamp":1640083298383
         },
         "internal_transactions":[
            
         ]
      },
      {
         "ret":[
            {
               "contractRet":"SUCCESS",
               "fee":0
            }
         ],
         "signature":[
            "631b0857da6d80da79702c7d31d8d3f2271f35523c7762231c28deeeda59bd1b17246adaabc1d9ebb717a2c3e25811efb31647d22c786e1651bdc8cb84365ead00"
         ],
         "txID":"c171d5fc085aac3ebaaa68280a72452f3b1a372cf605ef3f5c593a555b7c8daa",
         "net_usage":436,
         "raw_data_hex":"0a02f38e220816e44bdfd96d9c3e40c8eef3efb52f529901436f6e67726174756c6174696f6e73206f6e2067657474696e67207468652061697264726f702c2077686963682063616e2062652072656465656d6564206174207569666f2e76697020313050414c203d323033322e3754525828e681ade5969ce88eb7e5be97e7a9bae68a95efbc8ce58fafe59ca87569666f2e76697020313050414c203d323033322e37545258e58591e68da229efbc895a74080212700a32747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e736665724173736574436f6e7472616374123a0a07313030333533331215416887baae81f63469cb41fb530f1e3bd30a2e2b321a15419b3c8777809cefab5c6116af884af6b2d674be7220ef4f70e899f0efb52f",
         "net_fee":0,
         "energy_usage":0,
         "blockNumber":32961442,
         "block_timestamp":1629369408000,
         "energy_fee":0,
         "energy_usage_total":0,
         "raw_data":{
            "data":"436f6e67726174756c6174696f6e73206f6e2067657474696e67207468652061697264726f702c2077686963682063616e2062652072656465656d6564206174207569666f2e76697020313050414c203d323033322e3754525828e681ade5969ce88eb7e5be97e7a9bae68a95efbc8ce58fafe59ca87569666f2e76697020313050414c203d323033322e37545258e58591e68da229efbc89",
            "contract":[
               {
                  "parameter":{
                     "value":{
                        "amount":10223,
                        "asset_name":"1003533",
                        "owner_address":"416887baae81f63469cb41fb530f1e3bd30a2e2b32",
                        "to_address":"419b3c8777809cefab5c6116af884af6b2d674be72"
                     },
                     "type_url":"type.googleapis.com/protocol.TransferAssetContract"
                  },
                  "type":"TransferAssetContract"
               }
            ],
            "ref_block_bytes":"f38e",
            "ref_block_hash":"16e44bdfd96d9c3e",
            "expiration":1629369464648,
            "timestamp":1629369404648
         },
         "internal_transactions":[
            
         ]
      }
   ],
   "success":true,
   "meta":{
      "at":1660233100523,
      "fingerprint":"T6atjwJFD2dfCrd8tGpJettk5B1t17FyRLgPs4qgZubyM9WMhKJAkgaTPnUHToWRX9PoBwkLV6Cmrkp1b3noGyVc3C2rRd",
      "links":{
         "next":"https://api.trongrid.io/v1/accounts/TQ82FS4og14am8BXqndHX8HrYYARrhmjBk/transactions?limit=3&only_confirmed=true&only_to=true&fingerprint=T6atjwJFD2dfCrd8tGpJettk5B1t17FyRLgPs4qgZubyM9WMhKJAkgaTPnUHToWRX9PoBwkLV6Cmrkp1b3noGyVc3C2rRd"
      },
      "page_size":3
   }
}

PHP Code

$url = 'https://api.trongrid.io/v1/accounts/TQ82FS4og14am8BXqndHX8HrYYARrhmjBk/transactions?only_confirmed=true&only_to=true&limit=3';

$content = file_get_contents($url);
$array = json_decode($content, true);

foreach ($array as $value) {
    
   echo $value{txID};
}

Although,i can get the id when i declare a specific object number. Example

foreach ($array as $value) {
        
       echo $value[0]{txID};
    }

CodePudding user response:

Your JSON is not an array, it's an object. The data property in this object is the array you want.

$obj = json_decode($content, true);

foreach ($obj['data'] as $value) {
   echo $value['txID'];
}
  • Related