2015-12-12 51 views
0

我想解析一個JSON文件有史以來第一次,並將信息插入我的數據庫。問題是這個對象/數組是多維的,所以$number = stats[0]正在獲取數組的每個級別的值。 This is a link to the JSON file。我想避免使用PHP,但我用PHP解析的data:值。 Here is a link to my output right now。如果你看看我的輸出結果,我只對8,C,J感興趣。 Pavelski和26.這些是我想要的數據庫中的值。用多維數組/對象解析Json。 PHP

$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); */ 

} 

謝謝

回答

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 />"; 
} 

重複的守門員所需數據

+0

謝謝!我需要做一些JSON練習haha –

+0

這裏是在行動。再次感謝! [鏈接](http://52.4.65.161/final_project/statistics.php) –