0

I am trying to parse a JSON file for the first time ever and insert the information into my database. The problem is that this object/array is multidimensional, so $number = stats[0] is getting values on each level of the array. This is a link to the JSON file. I want to avoid everthing but the data: values that I exploded with PHP. Here is a link to my output right now. If you look at my output, I am only interested in the 8,C,J. Pavelski and 26. Those are the values I want in my database.

$json = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/playerstatsline/20152016/2/SJS/iphone/playerstatsline.json");

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
      $stats = explode(",", $val);

        $number = $stats[0];
        $position = $stats[1];
        $name = $stats[2];
        $gp = $stats[3];
        $goals = $stats[4];
        $assists = $stats[5];
        $points = $stats[6];
        $plsmns = $stats[7];
        $pim = $stats[8];
        $shots = $stats[9];
        $toi = $stats[10];
        $pp = $stats[11];
        $sh = $stats[12];
        $gwg = $stats[13];
        $ot = $stats[14];


        echo $number."    ".$position."    ".$name."    ".$points."<br />";

/* $query = "INSERT INTO stats2015_2016 ('number','position','name','gp','goals','assists','points','plsmns','pim','shots', 'toi', 'pp', 'sh', 'gwg', 'ot')
             VALUES ('$number','$position','$name','$gp','$goals','$assists','$points','$plsmns','$pim','$shots','$toi','$pp','$sh','$gwg','$ot')";
   $result= $db->query($query); */

}

Thank you

1 Answer 1

1
$json = file_get_contents('http://nhlwc.cdnak.neulion.com/fs1/nhl/league/playerstatsline/20152016/2/SJS/iphone/playerstatsline.json');

$json = json_decode($json, TRUE);

$skaterData = $json['skaterData'];
$goalieData = $json['goalieData'];

foreach($skaterData as $d){
    $sk = explode(',', $d['data']);
    $number = $sk[0];
    $position = $sk[1];
    $name = $sk[2];
    $points = $sk[6];

    echo $number."    ".$position."    ".$name."    ".$points."<br />";
}

repeat for goalie data as required

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I need to do some JSON practice haha
Here it is in action. Thanks again! Link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.