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'];
Related
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
i cant for the life of me figure out why i cant get this JSON to work
this is how it comes back from api
"{"errorCode":5,"errorDescription":"Unknown account"}"
i have tried all of the following to get "Unknown account"
$error = $result['errorCode":5,"errorDescription'];
$error = $result['errorCode'];
$error = $result['errorDescription'];
i would be ok even just trying for error code 5, so i can do a check to further the script if account is made or not.
any ideas what i am missing?
try this:
$json = '{"errorCode":5,"errorDescription":"Unknown account"}';
$arr = json_decode($json, true);
$error = $arr['errorDescription'];
In this case you take the json and parse it into the function json_decode of php, wit true value as second parameter It return an associative error where you can get your value by key.
DEMO
It's actually a simple matter as there are already builtin functions in PHP for handling JSON. Those are json_encode and json_decode.
$result='{"errorCode":5,"errorDescription":"Unknown account"}';
var_dump($result);
$json=json_decode($result);
var_dump($json);
echo "errorCode={$json->errorCode}<br>";
echo "errorDescription={$json->errorDescription}<br>";
Try the above. Note that json_decode can convert a string into either an object (default) or an associative array.
$json2=json_decode($result,TRUE);
var_dump($json2);
echo "errorCode=".$json2['errorCode']."<br>";
echo "errorDescription=".$json2['errorDescription']."<br>";
I made this question 3 days ago, but unfurtunaly I couldn't solve my problem till now. I will formulate the question again with the hope that somebody help me.
I have the following JSON structure
{"Id":"1","Persons":[{"Name":"Luis","Time":"00:00:09","info":"","Timeext":"","Timeout":"","Timein":""}, {"Name":"Carl","Time":"00:00:03","info":"","Timeext":"","Timeout":"","Timein":""},{"Name":"Ben","Time":"00:00:08","info":"","Timeext":"","Timeout":"","Timein":""}]}
To the element Id is not a problem to access. I can get this value like this:
$arr['Id'] = $_POST['Id'];
echo $arr['Id'];
But if want to access to the structure Persons inside of the JSON, specifically to the Time value of each Person, I do like this:
$arr['Persons'] = $_POST['Persons'];
$jsdecode = json_decode($arr['Persons']);
foreach ($arr['Persons'] as $p){
echo "$p->Time <br/>";
}
And this is the result that get:
<b>Warning</b>: json_decode() expects parameter 1 to be string, array given in <b>C:\xampp\htdocs\Stopuhr\controller\prozess.controller.php</b> on line <b>38</b><br />
<br />
<b>Notice</b>: Trying to get property of non-object in <b>C:\xampp\htdocs\Stopuhr\controller\prozess.controller.php</b> on line <b>41</b><br />
<br/><br />
<b>Notice</b>: Trying to get property of non-object in <b>C:\xampp\htdocs\Stopuhr\controller\prozess.controller.php</b> on line <b>41</b><br />
<br/>
Can PLEASE somebody help me?
json_encode() - PHP Array to JSON
The json_encode() method will take a PHP array and encode it as JSON ready to be consumed by an AJAX call.
$myarray = array('Guitar' => 'Johnny', 'Vocals'=> 'Stephen', 'Bass' => 'Andy', 'Drums' => 'Mike');
$myJson = json_encode($myarray);
echo $myJson;
json_decode() - JSON to PHP Array
json_decode() will take JSON and convert it into a PHP array.
$myJson = '{"Guitar" : "Johnny", "Vocals": "Stephen", "Bass" : "Andy", "Drums" : "Mike"}';
$myarray = json_decode($myJson, true);
print_r($myarray);
You have a syntax error:
{"Id":"1","Persons":[
{"Name":"Luis","Time":"00:00:09","info":"","Timeext":"","Timeout":"","Timein":""},
{"Name":"Carl","Time":"00:00:03","info":"","Timeext":"","Timeout":"","Timein":""},
{"Name":"Luis","Time":"00:00:08","info":"","Timeext":"","Timeout":"","Timein":""}
]}
no comma before Name Louis.
You should always validate your encoded json on http://jsonlint.com/
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"];
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