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',
)
I've got a problem. I can't find a proper array in this json file. When i try this:
<?php
$jsonurl = "http://steamcommunity.com/profiles/76561198132044757/inventory/json/304930/2";
$json = file_get_contents($jsonurl);
$arJson = json_decode( $json, true );
echo $arJson[0]
?>
it says:
Notice: Undefined offset: 0
or if i try this:
echo $arJson["rgInventory"]
it says:
Notice: Array to string conversion
Where are the arrays in my json file and how to adress them?
Thank you in advance and sorry for my bad english ;)
Jonas
Notice: Undefined offset: 0
You get this error because your array is an associative array, that mean's there is no index 0. For more info take a look at the docs.
Notice: Array to string conversion
You get this error, because echo can only output strings not arrays
to see what's in your array, you can use var_dump()
<?php
$jsonurl = "http://steamcommunity.com/profiles/76561198132044757/inventory/json/304930/2";
$json = file_get_contents($jsonurl);
$arJson = json_decode( $json, true );
echo "<pre>";
var_dump( $arJson );
$arJson["rgInventory"] is also an array so you can see the values with:
var_dump( $arJson["rgInventory"] );
1) First of all, you should read Steam API documentation to know the structure of coming data.
https://steamcommunity.com/dev
2) Use print_r and var_dump functions to see the structure of your variables.
For example <pre> <? print_r($arJson) ?>
you have to use print_r in order to print an array.
See your result with
echo '<pre>';print_r($arJson);
I am using the following code to get the number of likes on a page.
<?php
$site="http://graph.facebook.com/?ids=http%3a%2f%2fXXXXXXXX/svce.php";
$graph= file_get_contents($site);
$json_string=$graph;
$array = json_decode($json_string, true);
//echo "<pre>";
//print_r($array);
$var = $array['shares'];
echo $var;
?>
But whenever i try to echo out the following code. I always get an unidentified index Notice which is as following: Notice: Undefined index: shares in C:\xampp\htdocs\graphapi.php on line 19
Where am i going wrong?
Here's the print_r output:
Array
(
[http://xxxxxxxxx/svce.php] => Array
(
[id] => http://xxxxxxxxx/svce.php
[shares] => 7
[comments] => 3
)
)
According your print out looks like there is an array more in $array. Try this;
echo $array['http://xxxxxxxxx/svce.php']['shares'];
You have to use the site-name as an key before.
Structure:
- http://example.com
- id
- shares
This means in PHP:
$array["http://example.com/path/to/site"]["shares"];
Original SQL query is this;
SELECT id,post_title,post_date FROM wp_posts where id='1'
When I retrieve the record, I am finding it but when it comes to returning the results, I am puzzled. Here is the where I got stuck.
while ($row = mysql_fetch_assoc($RS)) :
print_r ($row);
list($id,$post_title,$post_date) = $row;
endwhile;
print_r ($row) outputs this;
Array ( [ID] => 1 [post_title] => Hello world! [post_date] => 2012-03-27 03:28:27 )
And when I run the list function in there ( for debug purposes obviously ), I get this;
Notice: Undefined offset: 2 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 1 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 0 in F:\inetpub\wwwroot\whatever\sql.php on line 147
What's causing this?
Replace:
mysql_fetch_assoc($RS)
with:
mysql_fetch_array($RS, MYSQL_NUM)
then it should work, because the list function trys to access the array using numeric keys.
I guess the answer lies somewhere within this;
list() only works on numerical arrays and assumes the numerical indices start at 0.
:(
You might be able to use extract() here instead, as well; (documentation here.)
You used mysql_fetch_assoc, so the resulting array per row has data under a key by column name, whereas "list" tries to match variables to values using numerical array indexes. You can use mysql_fetch_array instead.
$categ = val1 | val2
list($one,$two,$three)=#split('[|]',$categ);
If you try to list the value which is not available it will return the error Undefined Offset.
Here the error will be Undefined Offset 2.
Because while spliting $categ, it will have only two values, if you try to access third value then it will return error.