How do I parse this JSON array in PHP

+2 votes

I've been playing around a little bit with the Bitcoin API and finally have it interacting with my local bitcoind server.

Now the following code:

$bitcoin->listreceivedbyaccount();

prints the following:

Array
(
    [0] => Array
        (
            [account] => root213
            [amount] => 1
            [confirmations] => 3
        )

)

How can I print or work with [account] or [amount] for example? Can someone please tell me?

Sep 3, 2018 in Blockchain by digger
• 26,740 points

reshown Sep 11, 2018 by Sudhir 6,333 views

5 answers to this question.

+1 vote
Best answer
/**
 *   Firstly collect the data 
 *   as an accessible variable
**/
$SomeVar = $bitcoin->listreceivedbyaccount();

/**
 *   Print the contents for just demonstration! 
 *   (Dont use print_r() in production!)
**/ 
print_r( $SomeVar );
Array
(
    [0] => Array
        (
            [account] => root213
            [amount] => 1
            [confirmations] => 3
        )

)

Access to 'Account'

echo $SomeVar[0]["account"]; //echos root213

Access to 'Amount'

echo $SomeVar[0]["amount"]; //echos 1
answered Sep 3, 2018 by slayer
• 29,370 points

selected Apr 26, 2019 by Omkar
+1 vote
$data = $bitcoin->listreceivedbyaccount();

$account = $data[0]['account'];
$amount = $data[0]['amount'];
answered Sep 3, 2018 by Murali
+1 vote
$arrJSON = $bitcoin->listreceivedbyaccount();
foreach($arrJSON as $arr) {
    print($arr['account']);
    print($arr['amount']);           
}
answered Sep 3, 2018 by anonymous
0 votes
/**
 * Convert a string, number, or object into an array.
 * Especially useful for objects such as those that
 * come from simplexml_load_file(), etc.
 *
 * @param mixed $non_array
 *   Any string, number, or object.
 *
 * @return
 *   An "arrayified" version of $non_array. At minimum,
 *   this should always return an empty array.
 */
function arrayify($non_array) {
  if (empty($non_array) && $non_array !== 0) {
    return array();
  }
  return unserialize(serialize(json_decode(json_encode((array) $non_array), 1)));
}

Then use it like this to extract the JSON data:

$data = arrayify($bitcoin->listreceivedbyaccount());
print_r($data);
answered Sep 4, 2018 by Lulu

reshown Sep 11, 2018 by Sudhir
0 votes

Here's one way, if you want to work with multiple values:

$data = $bitcoin->listreceivedbyaccount();
$count = count($data);
// Avoid errors
$amounts = array();
// Avoid errors
$confirmations = array();
for ($i = 0; $i < $count; $i++) {
  $amounts[] = $data[$i]['amount'];
  $confirmations[] = $data[$i]['confirmations'];
}
foreach ($amounts as $amount) {
  // Do something, like:
  // print $amount;
}
answered Sep 4, 2018 by Tiffany

reshown Sep 11, 2018 by Sudhir

Related Questions In Blockchain

0 votes
1 answer

How do I initialize an array in a struct

You need to manually change the length ...READ MORE

answered Oct 1, 2018 in Blockchain by Perry
• 17,100 points
3,011 views
+4 votes
2 answers

How do I create a new block in Hyperledger Fabric?

This link might help you: https://github.com/hyperledger/fabric-sample ...READ MORE

answered Oct 11, 2018 in Blockchain by Sahu
2,655 views
+1 vote
1 answer

How do i change the names of validating peers in IBM Bluemix blockchain?

IBM Bluemix Blockchain service Hyperledger Fabric v0.6 will ...READ MORE

answered Apr 11, 2018 in Blockchain by Perry
971 views
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,740 points
45,219 views
0 votes
1 answer

Extract data from Json url result php

Try this code, it worked for me: $balance ...READ MORE

answered Sep 3, 2018 in Blockchain by slayer
• 29,370 points
9,799 views
0 votes
1 answer

How do I add multiple recipients for transactions via Blockchain API?

Convert the recipes into JSON objects. x = ...READ MORE

answered Jul 6, 2018 in Blockchain by Perry
• 17,100 points
886 views
0 votes
1 answer

Cant connect Jsonrpc to bitcoin.

I tried the following code and it ...READ MORE

answered Aug 20, 2018 in Blockchain by digger
• 26,740 points
1,008 views
+1 vote
2 answers
0 votes
1 answer

How do nodes communicate with each other in hyperledger?

First let me tell you that there ...READ MORE

answered Jul 17, 2018 in Blockchain by slayer
• 29,370 points
1,523 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP