i have a json file, that has to be parsed in a loop.
i cant seem to succeed
JSON:
{"IMD":{"url":"http:\/\/www.google.com","timeOut":1515155361},"cvH":{"url":"http:\/\/www.google.com","timeOut":1515155364}}
PHP:
<?php
$linkyValues="./linky.json";
if (file_exists($linkyValues)) {
$fileStream = fopen($linkyValues, 'r');
$fileValue=json_decode(fread($fileStream, filesize($linkyValues)));
fclose($fileStream);
echo count($fileValue);//Always 1!
for($i=0;$i<count($fileValue);$i++){
$timeout=$fileValue->item($i)->timeOut;
if(time()>=$timeout){
unset($fileValue[$i]);
}
}
$fileStream = fopen($linkyValues, 'w');
fwrite($fileStream, json_encode($fileValue));
fclose($fileStream);
}
?>
my problem is that count($fileValue) is always 1.
Output of var_dump($fileValue):
object(stdClass)#2 (2) {
["IMD"]=>
object(stdClass)#1 (2) {
["url"]=>
string(21) "http://www.google.com"
["timeOut"]=>
int(1515155361)
}
["cvH"]=>
object(stdClass)#3 (2) {
["url"]=>
string(21) "http://www.google.com"
["timeOut"]=>
int(1515155364)
}
}
it looks like an array to me...
JSON does not support the concept of an associative array, but stores such maps as objects instead.
Your JSON file contains such an object. For PHP this means, that it can either import it as an stdClass ( object ) or as an associatiave array.
This is decided by the json_decode's second parameter, that is either TRUE to read the object as an associative array, or FALSE to read it as an object.
Therefore this will fix your problem:
$fileValue = json_decode(fread($fileStream, filesize($linkyValues)), TRUE);
json_decode documentation
In addition to that, your code has problems with iterating the array. You use $fileValue->item($i) as well as $fileValue[$i], while you have an associative array.
You work with it as if it was an indexed array, while it is an associative array, which means it has keys instead of indices, that identify the values in your array.
The propper way to iterate an associative array is with foreach, like deomstrated belo:
foreach($fileValue as $key => $value) {
if (time() >= $value['timeOut']) {
unset($fileValue[$key]);
}
}
Yet, since you only want to remove specific values, you can use array_filter as well:
$fileValue = array_filter($fileValue, function($value){
return time() < $value['timeOut'];
});
array_filter will then take care of removing the specified fields from your array, so you do not have to unset them manually.
You can use :
count((array)$fileValue);
You're mixing up arrays and objects here.
count() can only be used on arrays, however you can cast an object to array to achieve the same thing.
$fileValue[$i] is a method to access an array, which won't work with your json object.
I see a solution already is to just change your object to an array so I'd like to offer the solution if you wanted to stick with objects.
$linkyValues="./linky.json";
if (file_exists($linkyValues)) {
$fileStream = fopen($linkyValues, 'r');
$fileValue=json_decode($jsonString);
fclose($fileStream);
//Cast the object to an array to get the count, but count isn't really requierd
echo count((array)$fileValue);
//loop through the object
foreach($fileValue as $key=>$fv){
//pull the timeout
$timeout=$fv->timeOut;
//do the check
if(time()>=$timeout){
//remove the timeout from the object
unset($fileValue->$key);
}
}
$fileStream = fopen($linkyValues, 'w');
fwrite($fileStream, json_encode($fileValue));
fclose($fileStream);
}
?>
Related
I'm not able to get the value of the image attribute in this (apparently valid) JSON object:
echo var_dump($result);
array(1) {
["images"]=>
array(1) {
[0]=>
array(1) {
["src"]=>
string(112) "http://staticf5a.diaadia.info/sites/default/files/styles/landscape_310_155/public/nota_periodistica/taxis_13.jpg"
}
}
}
$jsonResult = json_encode($result); //result is an array of arrays
echo $jsonResult;
{"images":[{"src":"http:\/\/staticf5a.diaadia.info\/sites\/default\/files\/styles\/landscape_310_155\/public\/nota_periodistica\/taxis_13.jpg"}]}
echo $jsonResult->images; //show nothing
This snippet was working few days ago, and logs (ini_set('display_errors', '0');) don't show anything related.
After encoding, $jsonResult is just a string and you won't be able to access any elements of encoded JSON without decoding it first.
Have a look at PHP's ``json_decode'' function: http://php.net/manual/pl/function.json-decode.php
It will convert the JSON back to associative array or object.
Anyway, I have no idea why you encode the associative array as JSON and try to access images there instead of just taking it from the array itself.
You are trying to get property from a string here. json_encode() returns the json representation of an object as a string. That string you can turn into an actuall object with json_decode()
I am getting the contents of a facebook api call that will return all the posts made to a facebook group. So all the posts/comments on that post.
$data = json_decode(file_get_contents($url2));
foreach($data as $post){
foreach $post as $subpost)
{
...etc
}
}
The problem I have here is that I will need to use 3 nested foreach loops in order to actually get the data I want. How do I get the first element of $data without having to use a foreach?
e.g. something like $data[0] (which doesn't work). How does a foreach loop iterate through an object so I can just manually write it since I only want 1 single object thats nested inside arrays.
edit
object(stdClass)#1 (2) { ["data"]=> array(25) { [0]=> object(stdClass)#2 (10)...
i want to access the final object that contains 10 pieces of data.
$data = json_decode(file_get_contents($url2), true);
will return an array and you can access it via an index like $data[0].
Try to use like below - just pass true in second argument , it will convert object into array so you can use it as array.
$data = json_decode(file_get_contents($url2), true);
foreach($data as $post){
foreach $post as $subpost)
{
...etc
}
}
You should do
$data = json_decode(file_get_contents($url2),true);
To convert it to a array then try
$data[0]
You can use $post instead of $data..
indexing of $post depends on what kind of data you are receiving through json.
If it is a 2-dimensional array,You can use $post[0]['index'] or if it is a single-dimensional array,you can use $post['index'], which will give you the exact indexed item.
I tried this code
$jsonlogcontents='{"buildings":"townhall","Army":{ "Paladins":{ "325648":0, "546545":4 }, "Knights":{ "325648":-2, "546545":0 } } }';
$phpArray = json_decode($jsonlogcontents, false);
echo $phpArray->buildings;
echo $phpArray->Army;
This is just a sample of my code, the JSON file is too large to include and has sensitive information. The problem I'm having is I can't get the value or print the value of
$phpArray->Army
It give's me an error. I can however print or get the value of
$phpArray->buildings
I'm thinking that when you decode a JSON file/contents in PHP, you can't get/print/store the value of a 'key' that has more set of info (more { and }) or brackets in it. You can only print/get values for keys who's value's contain only 1 value and nothing else.
If this is the case, what can I do to get the contents of the key that has more JSON codes in it. Also, how can I convert the contents of a key that has more JSON info in it into a string? the conversion is so I can display the value of that key to the page or echo it
The error is because Army is an object, and echo doesn't know how to convert it to a string for display. Use:
print_r($phpArray->Army);
or:
var_dump($phpArray->Army);
to see its contents.
P.S. $phpArray not an array but an object.
For Army however, I will need to do another json_decode() for that.
You don't. json_decode() decodes the entire structure in one call, into one large object (or array). No matter how deeply nested the data is, you call json_decode() once and you're done. That's why Army is not a JSON string any more.
When you are adding false as the second parameter to the json_encode it will updating all array to the sdClass empty objects.In this way you can the main array as the object
<?php
$json = '{
"buildings": "townhall",
"Army": {
"Paladins": {
"325648": 0,
"546545": 4
},
"Knights": {
"325648": -2,
"546545": 0
}
}
}';
$array = json_decode($json, true);
$object = (object)$array;
var_dump($object->Army);
?>
OUTPUT
array(2) {
["Paladins"]=>
array(2) {
[325648]=>
int(0)
[546545]=>
int(4)
}
["Knights"]=>
array(2) {
[325648]=>
int(-2)
[546545]=>
int(0)
}
}
Working Demo
It's because the output from your json_decode looks like this:
object(stdClass)(
'buildings' => 'townhall',
'Army' => object(stdClass)(
'Paladins' => object(stdClass)(
325648 => 0,
546545 => 4
),
'Knights' => object(stdClass)(
325648 => -2,
546545 => 0
)
)
)
Army is a standard object so it can't know how to echo it. You can however var_dump it:
var_dump($phpArray->Army);
The easiest solution is to treat it as a normal array with:
$phpArray = json_decode($jsonlogcontents, true);
how to read below json data in php?
i have "$json = json_decode($data,true); and
i tryied "$json->{'screenShareCode'};" but is is giving me an error? :(
array(5) {
["screenShareCode"]=>
string(9) "887874819"
["appletHtml"]=>
string(668) ""
["presenterParams"]=>
string(396) "aUsEdyygd6Yi5SqaJss0="
["viewerUrl"]=>
string(65) "http://api.screenleap.com/v2/viewer/814222219?accountid=myid"
["origin"]=>
string(3) "API"
}
The output you are showing is not json. It seems to be a print_r'ed array.
See http://json.org/example
Your output is regular array not JSON, so you access it as regular PHP array:
$x = $array['screenShareCode']
You are looking for json_encode (http://de2.php.net/json_encode)
What you posted is an array, not an object. Because you passed json_decode a second parameter of true, it responded with an associative array instead of an object.
To access a property of an associative array, you can do something like $json['screenShareCode'].
I have an array and PHP and when I print it out I can see the values I need to access, but when I try accessing them by their key I am getting a PHP Notice. I printed the array with print_r:
Array
(
[207] => sdf
[210] => sdf
)
When I try to access the array using the index I get an undefined offset notice. Here is my code:
print_r($output);
echo $output[207]; // Undefined Offset
echo $output["207"]; // Undefined Offset
The $output array is the result of a call to array_diff_key and is input originally as JSON through an HTTP POST request.
array_keys gives me the following:
Array
(
[0] => 207
[1] => 210
)
In response to the comments:
var_dump(key($output)); outputs:
string(3) "207"
var_dump(isset($output[key($output)])); outputs:
bool(false)
See this section on converting an object to an array in the PHP Manual:
The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name.
When converting to an array from an object in PHP, integer array keys are stored internally as strings. When you access array elements in PHP or use an array normally, keys that contain valid integers will be converted to integers automatically. An integer stored internally as a string is an inaccessible key.
Note the difference:
$x = (array)json_decode('{"207":"test"}');
var_dump(key($x)); // string(3) "207"
var_dump($x);
// array(1) {
// ["207"]=>
// string(4) "test"
// }
$y['207'] = 'test';
var_dump(key($y)); // int(207)
var_dump($y);
// array(1) {
// [207]=>
// string(4) "test"
// }
print_r on both those arrays gives identical output, but with var_dump you can see the differences.
Here is some code that reproduces your exact problem:
$output = (array)json_decode('{"207":"sdf","210":"sdf"}');
print_r($output);
echo $output[207];
echo $output["207"];
And the simple fix is to pass in true to json_decode for the optional assoc argument, to specify that you want an array not an object:
$output = json_decode('{"207":"sdf","210":"sdf"}', true);
print_r($output);
echo $output[207];
echo $output["207"];
The problem arises when casting to array an object that has string keys that are valid integers.
If you have this object:
object(stdClass)#1 (2) {
["207"]=>
string(3) "sdf"
["210"]=>
string(3) "sdf"
}
and you cast it with
$array = (array)$object
you get this array
array(2) {
["207"]=>
string(3) "sdf"
["210"]=>
string(3) "sdf"
}
which has keys that can only be accessed by looping through them, since a direct access like $array["207"] will always be converted to $array[207], which does not exist.
Since you are getting an object like the one above from json_decode() applied to a string like
$json = '{"207":"sdf", "210":"sdf"}'
The best solution would be to avoid numeric keys in the first place. These are probably better modelled as numeric properties of an array of objects:
$json = '[{"numAttr":207, "strAttr":"sdf"}, {"numAttr":210, "strAttr":"sdf"}]'
This data structure has several advantages over the present one:
it better reflects the original data, as a collection of objects
which have a numeric property
it is readily extensible with other properties
it is more portable across different systems
(as you see, your current data structure is causing issues in PHP, but if you
should happen to use another language you may easily encounter
similar issues).
If a property → object map is needed, it can be quickly obtained, e.g., like this:
function getNumAttr($obj) { return $obj->numAttr; } // for backward compatibility
$arr = json_decode($json); // where $json = '[{"numAttr":...
$map = array_combine(array_map('getNumAttr', $arr), $arr);
The other solution would be to do as ascii-lime suggested: force json_decode() to output associative arrays instead of objects, by setting its second parameter to true:
$map = json_decode($json, true);
For your input data this produces directly
array(2) {
[207]=>
string(3) "sdf"
[210]=>
string(3) "sdf"
}
Note that the keys of the array are now integers instead of strings.
I would consider changing the JSON data structure a much cleaner solution, though, although I understand that it might not be possible to do so.
I've just found this bug which causes array elements to be inaccessible sometimes in PHP when the array is created by a call to unserialize.
Create a test PHP file containing (or run from the command line) the following script:
<?php
$a = unserialize('a:2:{s:2:"10";i:1;s:2:"01";i:2;}');
print $a['10']."\n";
$a['10'] = 3;
$a['01'] = 4;
print_r($a);
foreach ($a as $k => $v)
{
print 'KEY: ';
var_dump($k);
print 'VAL: ';
var_dump($v);
print "\n";
}
If you get errors you have a version of PHP with this bug in it and I recommend upgrading to PHP 5.3
Try
var_dump($output);
foreach ($output as $key => val) {
var_dump($key);
var_dump($val);
}
to learn more on what is happening.
What exact line/statement is throwing you a warning?
How did you print the array? I would suggest print_r($arrayName);
Next, you can print individual elements like: echo $arrayName[0];
Try use my approach:
class ObjectToArray {
public static function convert( $object ) {
if( !is_object( $object ) && !is_array( $object ) ) {
return $object;
}
if( is_object( $object ) ) {
$object = get_object_vars( $object );
}
return array_map( 'ObjectToArray::convert', $object );
}
}
$aNewArray = ObjectToArray::convert($oYourObject);
Just put error_reporting(0); in you method or at start of file. It will solved your issue.