Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Illegal string offset

Tags:

php

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?

like image 688
Convertor Avatar asked Nov 28 '22 01:11

Convertor


1 Answers

$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'];
like image 105
Amal Murali Avatar answered Dec 09 '22 02:12

Amal Murali