i have below json code
{"1":1,"5":1}
when i decode the above json i got object array using the below php statement.
$array_val = (array)json_decode($price);
i got a below array.
Array
(
[1] => 1
[5] => 1
)
but the below statement does not work
echo $array_val[1];
the below error occurred.
Undefined offset: 1
How to resolve this issue?
try this DEMO
PHP
$json = '{"1":1,"5":1}';
$array_val=json_decode($price, true);
echo $array_val[1];
OUTPUT :
1
Note that json_decode($string) returns an object, not an array (which is why your code doesn't behave).
To return an array instead, use:
$arr = json_decode($string, true);
See also http://php.net/manual/en/function.json-decode.php
You can get this using below code
$array_val=json_decode($price);
echo $array_val->{1}
OR
$array_val=json_decode($price,true);
echo $array_val[1]
I'm trying to parse this JSON object and iterate through it to make a table in html. At the trials, I can't property echo the values. How do I do that correctly?
{"1":{"1":"Employer+EID","2":"File+Creation+Date","3":"File+Creation+Time","4":"Payer+EID","5":"Payer+QID","6":"Payer+Bank+Short+Name","7":"Payer+IBAN","8":"Salary+Year+and+Montd","9":"Total+Salaries","10":"Total+records","11":"","12":"","13":"","14":"","15":""},"2":{"1":"12435800","2":"20160714","3":"0318","4":"12435800","5":"","6":"DBQ","7":"QA79DOHB021104613880010010000","8":"201606","9":"183941.22166664","10":"113","11":"","12":"","13":"","14":"","15":""},"3":{"1":"Record+ID","2":"Employee+QID","3":"Employee+Visa+ID","4":"Employee+Name","5":"Employee+Bank+Short+Name","6":"Employee+Account","7":"Salary+Frequency","8":"Number+of+Working+Days","9":"Net+Salary","10":"Basic+Salary","11":"Extra+hours","12":"Extra+Income","13":"Deductions","14":"Payment+Type","15":"Notes/+Comments"},"4":{"1":"1","2":"27835620341","3":"","4":"SHIJAN+THARAKAN+THOMAS","5":"DBQ","6":"2025","7":"M","8":"30","9":"7300","10":"5000","11":"0.00","12":"2500.00000000","13":"200","14":"","15":""}}
I tried:
$data = json_decode($_POST['data'],true);
//echo count($data);
echo ($data[4][2]);`
The result was a null JSON. How can I do this correctly?
Your JSON String is -
$str = '{"1":{"1":"Employer+EID","2":"File+Creation+Date","3":"File+Creation+Time","4":"Payer+EID","5":"Payer+QID","6":"Payer+Bank+Short+Name","7":"Payer+IBAN","8":"Salary+Year+and+Montd","9":"Total+Salaries","10":"Total+records","11":"","12":"","13":"","14":"","15":""},"2":{"1":"12435800","2":"20160714","3":"0318","4":"12435800","5":"","6":"DBQ","7":"QA79DOHB021104613880010010000","8":"201606","9":"183941.22166664","10":"113","11":"","12":"","13":"","14":"","15":""},"3":{"1":"Record+ID","2":"Employee+QID","3":"Employee+Visa+ID","4":"Employee+Name","5":"Employee+Bank+Short+Name","6":"Employee+Account","7":"Salary+Frequency","8":"Number+of+Working+Days","9":"Net+Salary","10":"Basic+Salary","11":"Extra+hours","12":"Extra+Income","13":"Deductions","14":"Payment+Type","15":"Notes/+Comments"},"4":{"1":"1","2":"27835620341","3":"","4":"SHIJAN+THARAKAN+THOMAS","5":"DBQ","6":"2025","7":"M","8":"30","9":"7300","10":"5000","11":"0.00","12":"2500.00000000","13":"200","14":"","15":""}}';
$result_array = json_decode($str, true);
echo '<pre>'; print_r($result_array);
echo $result_array[4][2];
It works fine for me. Please check you $_POST['data'] variable. It may undefined debug this variable by print_r($_POST); if this data index is defined you will see an index of data such as -
Array
(
['data'] => 'some_value',
)
Am trying to get more familiar with JSON decoding, so here's what I got:
$json = '{"id":[{"tier": "SILVER"}]}';
$array = json_decode($json, true);
var_dump($array["id"]['tier']);
I am trying to fetch the 'tier', but it's resulting the following error: Notice: Undefined index: tier
I have tried certain things such as var_dump($array['tier']);, var_dump($array[0]['tier']); but nothing seems to work and I can not find a lot of information about this.
After the Noticeit also returns NULL. Any help is appreciated.
The id key in the resulting array will contain an numerically indexed array of arrays.
To access, you need to specify the key in that array, in this case 0 as there is only a single element
var_dump($array["id"][0]['tier']);
If you where to decode to an object rather than forcing an associate array (by omitting the true in the json_decode call), you might find the syntax a little easier to read:
$json = '{"id":[{"tier": "SILVER"}]}';
$obj = json_decode($json);
var_dump($obj->id[0]->tier);
You need to access the first item [0]on the array ["id"]
Try this:
$json = '{"id":[{"tier": "SILVER"}]}';
$array = json_decode($json, true);
var_dump($array["id"][0]['tier']);
//string(6) "SILVER"
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'];
Trying to use some decoded json data, but I'm unable to extract it to use. I've looked at other examples that should work, but haven't worked for me.
What am I missing?
(I'm trying to do what is in the first answer of How to parse json response from CURL )
Raw JSON
{"CustomerOriginId":123456}
JSON Decode:
$result = json_decode($head, true);
Print_R results (print_r($result);):
Array ( [CustomerOriginId] => 123456 )
Var_Dump results (var_dump($result);):
array(1) { ["CustomerOriginId"]=> int(123456) }
My attempts to extract the data for use:
Attempt 1
Attempt 1 Code:
$test45 = $result["CustomerOriginID"];
echo $test45;
Attempt 1 Error:
Notice: Undefined index: CustomerOriginID
Attempt 2
Attempt 2 Code:
$test46 = $result['CustomerOriginID'];
echo $test46;
Attempt 2 Result:
Notice: Undefined index: CustomerOriginID
Attempt 3
Attempt 3 Code:
$test47 = $result[0]['CustomerOriginID'];
echo $test47;
Attempt 3 Result:
Notice: Undefined offset: 0
Attempt 4
Attempt 4 Code:
$test48 = $result[1]['CustomerOriginID'];
echo $test48;
Attempt 4 Result:
Notice: Undefined offset: 1
I'm sure it's something small, but I haven't found an answer as of yet.
Cheers!
Undefined Index usually means you are accessing the array value the wrong way.
The index must match CustomerOriginId or it will be undefined.
Try this:
$json='{ "CustomerOriginId" : 123456 }';
$result = json_decode($json, true);
$CustomerOriginId = $result['CustomerOriginId'];
echo 'CustomerOriginId = '.$CustomerOriginId;
or without associative array
$json='{ "CustomerOriginId" : 123456 }';
$result = json_decode($json);
$CustomerOriginId = $result->CustomerOriginId;
echo 'CustomerOriginId = '.$CustomerOriginId;
The index for your array is "CustomerOriginId", not "CustomerOriginID" (note the case).
$json = '{"CustomerOriginId":123456}';
$array = json_decode($json, true);
print $array['CustomerOriginId']; // outputs 123456
this works for me:
$x = json_decode('{"CustomerOriginId":123456}', true);
print_r($x);
print $x['CustomerOriginId'];
output:
Array
(
[CustomerOriginId] => 123456
)
123456