Viewed   167 times

Take this code:

$json = file_get_contents($this->url, true); 
$decode = json_decode($json, true); 

foreach ($decode as $key => $value) {
 ...
}

Pretty simple, uh?

Passing a $json with up to 500 array elements.... works right!

Above that limit... the error is:

Warning: Invalid argument supplied for foreach() in /c/website/retriever/WsGlassRetriever.php on line 19

Is there some memory limit for that function's argument?

I didn't found nothing about it in the docs. My version is PHP 5.2.17-rnx1.1 with Suhosin-Patch 0.9.7 (cli)

 Answers

2

json_decode returns NULL if there is an error in the JSON syntax. I've just successfully tested on an array of 1000 elements and it ran just fine.

Double-check that your JSON is correctly formatted. Even something as small as having single quotes instead of double, or forgetting to put a property name in quotes, or using a character outside the 32-127 range without correctly encoding it in UTF-8 can cause these problems.

Wednesday, September 14, 2022
 
5

You can access object properties with names that contain special characters using this notation:

$json->NBBList->{'$values'}[0]

I don't think that this behavior is documented anywhere, but you can find it in the PHP grammar (see definition of variable_name, which is used in object_dim_list, which is used in object_property).

Wednesday, September 14, 2022
 
1

json_decode takes a string as an argument. Read in the file with file_get_contents

$json_data = file_get_contents('data.txt');
json_decode($json_data, true);

You do need to adjust your sample string to be valid JSON by adding quotes around strings, commas between objects and placing the objects inside a containing array (or object).

[{"name":"yekky"}, {"name":"mussie"}, {"name":"jessecasicas"}]
Friday, August 5, 2022
 
rpmx
 
1

Given this JSON, you can get the currency of a country as follows:

function getCurrencyFor($arr, $findCountry) {
    foreach($arr as $country) {
        if ($country->name->common == $findCountry) {
            $currency = $country->currency[0];
            break;
        }
    }
    return $currency;
}

$json = file_get_contents("https://raw.githubusercontent.com/mledoze/countries/master/countries.json");
$arr = json_decode($json);
// Call our function to extract the currency for Angola:
$currency = getCurrencyFor($arr, "Angola");

echo "Angola has $currency as currency";
Sunday, October 30, 2022
 
4

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"/meta/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
{
    echo "true";    
}
else {
    echo "false";   
}
Thursday, October 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :