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
• 27,620 points

reshown Sep 11, 2018 by Sudhir 773 views

5 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+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,040 points

selected Apr 26 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
• 16,450 points
27 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
381 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 10, 2018 in Blockchain by Perry
72 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
• 27,620 points
1,832 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,040 points
79 views
0 votes
1 answer
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,040 points
98 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.