PHP:非法字符串偏移量 [英] PHP: Illegal string offset

查看:518
本文介绍了PHP:非法字符串偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有这样的问题,但这并没有帮助我.

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

我的网站上出现以下错误:

I get the follow error on my site:

警告:中的非法字符串偏移量'networkConnections' /var/www/bitmsg/templates/header.php,第25行{

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

该行是 <?= $bmstatus["networkConnections"] ?> p2p nodes

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

如果我print_r $ bmstatus,则得到:

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?

我已经尝试了$bmstatus['networkConnections']$bmstatus->networkConnections 但是两者都返回了该错误?

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

推荐答案

$bmstatus包含JSON字符串.您必须先对其进行解码,然后才能从中提取所需的信息.为此,您可以使用内置函数 json_decode() (第二个参数设置为TRUE获取关联数组,而不是对象):

$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'];

这篇关于PHP:非法字符串偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆