2014-10-10 48 views
0

我試圖做一個循環通過一個非常大的JSON文件與其他人幫助我的這個功能。但是,下面的代碼將返回:Warning: Invalid argument supplied for foreach()評估php內部的函數,如果

此代碼的函數之前被執行:

$file = new SplFileObject("bdfile/summoner_leagues_entries_export.json"); 
while (!$file->eof()) 
{ 
    $json = json_decode($file->fgets(), true); 
    var_dump($json); 
} 

_

function getentry($json) 
{ 
     foreach($json as $league) 
     { 
       if($league['queue'] == 'RANKED_SOLO_5x5') 
       { 
        return $league['entries'][0]; 
       } 

     return null; 
     } 
} 
$entry = getentry($json); 
if(isset($entry)) echo $entry['playerOrTeamName'] . ',' . "</br>"; 

這是var_dump($json);輸出(只是一個很小的例子,因爲它的大文件):

{ 
    "_id" : ObjectId("540449a4f59600af7d285075"), 
    "leagues" : [{ 
     "name" : "Cassiopeia's Hunters", 
     "tier" : "GOLD", 
     "queue" : "RANKED_SOLO_5x5", 
     "entries" : [{ 
      "playerOrTeamId" : "21893177", 
      "playerOrTeamName" : "JoKoksa", 
      "division" : "III", 
      "leaguePoints" : NumberLong(5), 
      "wins" : NumberLong(99), 
      "isHotStreak" : false, 
      "isVeteran" : false, 
      "isFreshBlood" : false, 
      "isInactive" : false, 
      "miniSeries" : false 
     }], 
     "id" : NumberLong(21893177) 
    }, { 
     "name" : "Kayle's Patriots", 
     "tier" : "BRONZE", 
     "queue" : "RANKED_TEAM_3x3", 
     "entries" : [{ 
      "playerOrTeamId" : "TEAM-ffbaccc0-b8c0-11e2-b67a-782bcb497d6f", 
      "playerOrTeamName" : "EloStechers", 
      "division" : "II", 
      "leaguePoints" : NumberLong(64), 
      "wins" : NumberLong(9), 
      "isHotStreak" : false, 
      "isVeteran" : false, 
      "isFreshBlood" : false, 
      "isInactive" : false, 
      "miniSeries" : false 
     }], 
     "id" : NumberLong(21893177) 
    }, { 
     "name" : "Cassiopeia's Infiltrators", 
     "tier" : "BRONZE", 
     "queue" : "RANKED_TEAM_5x5", 
     "entries" : [{ 
      "playerOrTeamId" : "TEAM-ffbaccc0-b8c0-11e2-b67a-782bcb497d6f", 
      "playerOrTeamName" : "EloStechers", 
      "division" : "II", 
      "leaguePoints" : NumberLong(60), 
      "wins" : NumberLong(11), 
      "isHotStreak" : false, 
      "isVeteran" : false, 
      "isFreshBlood" : false, 
      "isInactive" : true, 
      "miniSeries" : false 
     }], 
     "id" : NumberLong(21893177) 
    }], 
    "summonerId" : NumberLong(21893177), 
    "region" : "euw", 
    "updatedAt" : NumberLong(1412719896) 
} 

編輯3:

$file = new SplFileObject("bdfile/summoner_leagues_entries_export.json"); 
    while (!$file->eof()) { 

     $json = json_decode($file->fgets(), true); 

      foreach($json['leagues'] as $entry) { 

      echo $entry['entries'][0]['playerOrTeamName'] . ',' . $entry['tier'] . ',' . $entry['entries'][0]['division'] . ',' . $entry['entries'][0]['leaguePoints'] . ',' . $entry['entries'][0]['wins'] . "<br/>"; 

      } 
    } 

$file = null; 

編輯4:

我得到它固定的,如果你有興趣,我可以在這裏粘貼代碼。

+0

這是奇怪的,你已經得到它解碼和VAR轉儲後你有另一個JSON字符串? – Ghost 2014-10-10 02:18:09

+0

我不知道,我只是試圖評估警告:'爲foreach()提供的無效參數,這讓我瘋狂。 – floppy 2014-10-10 02:21:17

+0

fck,我注意到這只是使用bson的mongodb的轉儲。你可以在'NumberLong()'上看到。但是,這不應該成爲foreach工作不正常的原因嗎?我成功地從同一個文件中解析了json而沒有if語句。但是,當我使用該foreach的if語句時,代碼停止工作。查看'EDIT 3'下的主代碼,查看相同JSON文件上的工作代碼。 – floppy 2014-10-10 02:28:51

回答

0

我覺得你在getentry線($ JSON)

foreach($json as $league) 

應該

foreach($json['leagues'] as $league) 
+0

我試着改變它,但不幸的是給了相同的結果。 'foreach($ json ['leagues']爲$聯盟)'' – floppy 2014-10-10 10:45:10

0

在反思。這看起來不像一個vardump。可能$ json只是一個字符串,當你進入getentry。

也許你需要一個bson_decode功能,您就可以把它像一個哈希(陣列)

json_decode不會處理它。

有一個在Mongo for php。

http://php.net/manual/en/function.bson-decode.php

但它確實說:「這個功能非常β和完全無用的,99%的用戶。如果你正在做一些奇怪的,如頂部寫自己的驅動程序是唯一有用的PHP驅動程序「。