PHP: Illegal string offset - 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?

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

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"];

Related

How to get data from json_encoded data from mysql in php?

I am storing data in json format in mysql database table column like
table A
column-user_details
"{\"name\":\"sadasfsf\",\"phone\":\"7896521747\",\"address_1\":\"dvgsdsd\",\"state_name\":\"g\",\"city_name\":\"sdgds\",\"zip_code\":\"ghdfh\"}"
I am fetching data like
json_decode($variable, true);
<?php echo $variable['name'];?>
However I am getting error like
Illegal string offset 'name'
the first parameter of json_decode is not a parameter passing by reference, so if you want to get the json code as php array, you mast adding the result of json_decode into a variable.
<?php
$variable = "{\"name\":\"sadasfsf\",\"phone\":\"7896521747\",\"address_1\":\"dvgsdsd\",\"state_name\":\"g\",\"city_name\":\"sdgds\",\"zip_code\":\"ghdfh\"}";
$array = json_decode($variable, true);
echo $array['name'];
NB: json_decode() is a php code, so you must use it after <?php tag, not outside of it

PHP parse multidimensional array with json

I'm trying to parse a json string extracted from a MySQL database. The json string contains a two-dimensional array, but json_decode returns null and json_last_error returns 0 which I assume means no error occurred.
$result is the json string
json_decode($result, true);
The string:
[
["17544500374","17544500489","17544500571","17544500587","17544500528"],
["17544500651","17544500432","17544500673","17544500452","17544500362"],
["17544500454","17544500457","17544500523","17544500441"],
["17544500547","17544500463","17544500535","17544500676"],
["17544500548","17544500581","17544500584","17544500382"],
["17544500593","17544500364","17544500660","17544500595"],
["17544500635","17544500647","17544500529","17544500670"]
]
You do not have a key for your values. That means, your second parameter is not correct, because the function is unable to create an associative array.
This works fine on my mashine:
json_decode($result);
The $result must be a string:
$result = '[["17544500374","17544500489","17544500571","17544500587","17544500528"],["17544500651","17544500432","17544500673","17544500452","17544500362"],["17544500454","17544500457","17544500523","17544500441"],["17544500547","17544500463","17544500535","17544500676"],["17544500548","17544500581","17544500584","17544500382"],["17544500593","17544500364","17544500660","17544500595"],["17544500635","17544500647","17544500529","17544500670"]]'

getting values out of json array in codeigniter

Hi am having a little from with json array. when i try to get the values out of json array.
Controller
$data['payment'] = $this->admin->get_payment_settings();
$value = $data['payment'][0]->json;
echo $value['username'];
Hi am having a json array in database. using my controller i am getting the filed json. When i do a var_dump($value) array look like this
{"username":"foodi1.lch.co","password":"FBUEWQH6X4D","signature":"AFcWxV21C7fd0v3bZnWKgr9on9AmTuhyd4MVq","currency":"CAD"}
i want to get each value out of this array.
echo $value['username'];
echo $value['password'];
When i try to do this i get the error
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'username'
Filename: controllers/administrator.php
Line Number: 620
Can some one help me to get the values out of json array. tnx..
You need to decode the string to a valid array, use json_decode as follows:
$json = json_decode($value, true);
echo $json['username'];
It seems your $data['payment'][0] is json string.So you need to decode it.
Try this way
$value=json_decode($data['payment'][0]->json,true);//second parameter true will return as array.
echo $value['username'];

Can't get value from array

I'm trying to output the value of the email value of an array, but have problems doing so.
The array is based on json_decode()
This is the error I receive
Fatal error: Cannot use object of type stdClass as array in /home/.... line 57
JSON (value of: $this->bck_content)
{"email":"test#email.com","membership_id":"0","fname":"Kenneth","lname":"Poulsen","userlevel":"1","created":"2012-04-23 10:57:45","lastlogin":"2012-04-23 10:58:52","active":"y"}
My code
# Display requested user details
$details_array = json_decode($this->bck_content);
$value = $details_array['email'];
print $value;
You need to use the second argument to json_decode to force array structures on JS objects.
json_decode($this->bck_content, true);
This will make sure all JS objects in the json are decoded as associative arrays instead of PHP StdObjects.
Of course that is assuming you want to use array notation to access them. If you're fine with using object notation then you can just use:
$value = $details_array->email;
try this one
$value = $details_array->email;
or
json_decode($json, true);
or
$details_array = (array)json_decode($json);
what have you done wrong is writen in error description

Trouble looping through an array created from a foursquare JSON feed

I'm working on a side project and at its core, I need to get a foursquare json feed into an array that i can loop through. My code is below and results in the following error:
Warning: Invalid argument supplied for foreach() in /homepages/7/d346835943/htdocs/dealrub/results.php on line 56
Here is an example of the json feed that i am correctly acquiring:
$jsonurl = "http://api.foursquare.com/v2/venues/search?ll=".$lat.",".$lon."&limit=100";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_encode($json, true);
foreach ( $json_output->response->groups[0]->items as $items )
{
echo "{$items->name}\n";
}
Any help as to what i'm doing wrong would be greatly appreciated. I left the jsonurl without my api key, but it is successfully returning the json results.
You have to use json_decode.
Check whether $json_ouput is not empty.
You are passing true as second argument to json_decode (assuming you have it right) which means that it returns an associative array.
Either omit that:
$json_output = json_decode($json);
or access items as array:
foreach ( $json_output['response']['groups'][0]['items'] as $items )
You're using json_encode on a string that is already in json. Try json_decode instead ;)

Categories