Alex Papadopoulos
Alex Papadopoulos

Reputation: 19

JSON Parsing issue with PHP

I'm attempting to pull in data from WoWProgress API. I want to parse the data online and just decode them directly while posting them for view. I'm still learning, but I seem to be having issues with this array. Would really need some help.

$json = file_get_contents("http://www.wowprogress.com/guild/eu/twisting-nether/hellenic%20horde/json_rank");
if($json == false)
{
throw new Exception("Failed To load infomation. Check setup options");
}
$result = json_decode($json, true);
echo "<pre>";
foreach ($result["realm_rank"] as $value) {
    print_r($value);
}
echo "</pre>";

But all i receive is "Invalid argument supplied for foreach()"

Would really love some help. Thanks in Advance!

Sorry for my english. English is not my native language.

Upvotes: 0

Views: 100

Answers (5)

Pupil
Pupil

Reputation: 23948

The $result['realm_rank'] is a string, not an array.

Therefore, foreach on it is causing error.

This error causes when either the array is not set, or is blank or not an array.

<?php
$json = file_get_contents("http://www.wowprogress.com/guild/eu/twisting-nether/hellenic%20horde/json_rank");
if ($json == false) {
 throw new Exception("Failed To load infomation. Check setup options");
}
$result = json_decode($json, true);
 if (isset($result['realm_rank'])) {
  if (is_array($result['realm_rank'])) {
   foreach ($result["realm_rank"] as $value) {
    echo "<pre>";
    print_r($value);
    echo "</pre>";
   }
  } 
  else {
   echo $result['realm_rank'];
  } 
}
else {
 echo 'Unknown error';
}
?>

Upvotes: 2

Professor Abronsius
Professor Abronsius

Reputation: 33804

Unless you specifically need to convert it to an array to loop through the values you could keep it in the default object format and access the values by name directly.

define('NL',PHP_EOL);/* only to prettify output */

$json = file_get_contents("http://www.wowprogress.com/guild/eu/twisting-nether/hellenic%20horde/json_rank");

if( !$json ) {
    throw new Exception("Failed To load infomation. Check setup options");
}

$json = json_decode( $json );

echo '<pre>',
    $json->score . NL, 
    $json->world_rank . NL, 
    $json->area_rank . NL, 
    $json->realm_rank . NL,
    '<pre>';

Upvotes: 0

P. Jairaj
P. Jairaj

Reputation: 1033

$result["realm_rank"] is not an array but a value. So you cant pass it in foreach.

Just do :

print_r($result["realm_rank"]);

I tried this and its working:

<?php

$json = file_get_contents("http://www.wowprogress.com/guild/eu/twisting-nether/hellenic%20horde/json_rank");
if($json == false)
{
throw new Exception("Failed To load infomation. Check setup options");
}
$result = json_decode($json, true);
echo "<pre>";
    print_r($result["realm_rank"]);
echo "</pre>";
?>

Upvotes: 0

devpro
devpro

Reputation: 16117

Result of $result is this

print_r($result);

Array
(
    [score] => 750000
    [world_rank] => 2117
    [area_rank] => 1031
    [realm_rank] => 44
)

Invalid argument supplied for foreach() means $result["realm_rank"] is not an array.

For solution you can use values as:

foreach($result as $value) {
    echo $value. "<br/>";
}

Or just use

echo $result["realm_rank"]; 

Upvotes: 0

Franz Gleichmann
Franz Gleichmann

Reputation: 3568

Your $result["realm_rank"] is not an array, but an integer. you can't iterate over an integer.

Try simply printing it:

print_r($result["realm_rank"]);

Upvotes: 0

Related Questions