4

I know there already are questions like this, but It didn't help me.

I get the follow error on my site:

Warning: Illegal string offset 'networkConnections' in /var/www/bitmsg/templates/header.php on line 25 {

The line is <?= $bmstatus["networkConnections"] ?> p2p nodes

if I print_r $bmstatus, then I get:

{
    "numberOfBroadcastsProcessed": 2308,
    "networkStatus": "connectedAndReceivingIncomingConnections",
    "softwareName": "PyBitmessage",
    "softwareVersion": "0.4.1",
    "networkConnections": 52,
    "numberOfMessagesProcessed": 22888,
    "numberOfPubkeysProcessed": 8115
}

How to I fetch the information from this array?

I've tried both $bmstatus['networkConnections'] and $bmstatus->networkConnections but both is returning that error?

1
  • user echo "<pre>"; var_dump($bmstatus) and show results Commented Nov 28, 2013 at 12:58

2 Answers 2

18

$bmstatus contains a JSON string. You have to decode it first to be able to extract the required information out of it. For this purpose, you can use the built-in function json_decode() (with the second parameter set as TRUE to get an associative array, instead of an object):

$json = json_decode($bmstatus, true);
echo $json['networkConnections'];
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, doh!, I knew that, I must be tired :) +1
@G33K1E: Glad to have been of help :)
9

It's a json string. You need to decode your json response using json_decode with second parameter true to get as an associative array.

$bmstatusArray = json_decode($bmstatus,true);
echo $bmstatusArray["networkConnections"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.