Jump to content

json split array


hyster

Recommended Posts

I have a feed which is in a json format. part of the script needs to be looped. ive worked out (I think) that I need to use foreach to split the data but I cannot find an example that works.

 

this is similar to what I have tried so far.

 $phpArray = json_decode($test);
 //print_r($phpArray);
 foreach ($phpArray as $key => {'data'}->{'vehicles'}->{'name'}->) { 
     echo "<p>$key | $value</p></br>";
 }

each iteration holds this data

 

{'data'}->{'vehicles'}->

 

        spotted
        localized_name
        name
        level
        damageDealt
        survivedBattles
        battle_count
        nation"
        image_url
        frags
        win_count
        class

 

the data that is fixed ie players name I can pull ok but not the data that's needs looping.

pointers gratefully accepted

 

Link to comment
https://forums.phpfreaks.com/topic/278211-json-split-array/
Share on other sites

Use http://jsoneditoronline.org/ to format your json to a readable file

Then look for a element that "[numbers]" beside it (e.g. in twitter json it is results[20]), usually that is where you gonna loop

 

Try this, but I'm not sure though because I don't know the structure of your JSON

$phpArray = json_decode($test);

foreach ($phpArray->vehicles as $item)
{
 // do what you want
 echo $item->spotted . '<br>';
 echo $item->localized_name . '<br>';
}
Link to comment
https://forums.phpfreaks.com/topic/278211-json-split-array/#findComment-1431209
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.