Jump to content

json split array


hyster
Go to solution Solved by Eiseth,

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
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.