2015-10-04 63 views
1

我正在爲撲克錦標賽構建一個排行榜(scorebored)。我的目標是將數據回顯到表格中,以顯示季節的分數和所有時間的分數。我希望桌子能夠以最高的賽季得分排列。 我得到的是一條錯誤信息:已聲明的鍵上未定義的索引錯誤

Notice: Undefined index: season in C:\wamp\www\UPT Site\leaders.php on line 11

當我print_r$allplayers數組的數組,它表明,所有的球員陣列正確進去,包括[season]鍵和值在線聲明6在下面的位...

任何人都可以請告訴我如何解決我的代碼? (請注意,真正的代碼中沒有行號,我只是在這裏添加它們來使討論更加容易)。

1 foreach($allplayers as $player){ 
2 $i = $player[1]; 

3 if (${"seasonplayerid" . $i}){ 

4 $sum = array_sum(${"seasonplayerid" . $i});} 
5 //$sum = points this season. 

6 ${"playerid" . $i}['season'] = $sum; 
7 } 

8 function val_sort($array,$key) {  
9 //Loop through and get the values of our specified key 
10 foreach($array as $k=>$v) { 
11 $b[] = strtolower($v[$key]); 
12 } 
13 asort($b); 
14 /* foreach($b as $k=>$v) { 
15  $c[] = $array[$k]; 
16 }return $c; 
17 */ 
18 } 
19 $sorted = val_sort($allplayers, '[season]'); 


20 foreach($allplayers as $player){ 
21 $i = $player[1]; 
22 echo ("<tr><td>" . $player[0] . $t . ${"playerid" . $i}[3] . $t . ${"playerid" . $i}[4] . $t. ${"playerid" . $i}['season'] . $t. count(${"seasonplayerid" . $i}). "</td><tr>"); 

23 } 

這裏是print_r輸出陣列$playerid1

陣列([0] =>喬納森湯普森[1] => 1 [2] => 2015-S 3 [3] => 944 [4] => 7 [季節] => 470)

以下爲在陣列中的信息的密鑰:

/* 
$allplayers is a multidimentional array, containing many arrays of players called $playerid1, $playerid2, $playerid3 etc 
     playerid1[0] = Player name 
     playerid1[1] = Player ID 
     playerid1[2] = Current season 
     playerid1[3] = total points earned 
     playerid1[4] = total games played games 
     playerid1[season] = points earned in current season 
*/ 
+1

包含您正在使用的數組,並且行號不是必需的;這裏的大多數人都可以算:) –

+1

在$ allplayers中發佈數據 – rocky

+1

@ Tunna182在'$ allplayers'陣列中沒有像'season'這樣的密鑰 – sandeepsure

回答

1

看起來像第19行,您嘗試傳遞第二個參數中的鍵,但是定義錯誤。因此,要撥打function val_sort($array,$key),您必須執行類似操作。

Therefore at line 19 change 

$sorted = val_sort($allplayers, '[season]'); 

$sorted = val_sort($allplayers, 'season'); 

此外,我建議你使用data table是又好又快

+0

你的答案和我的答案有什麼區別? – sandeepsure

+0

@sandeepsure你錯過了解釋 – rocky

+0

它已經被代碼理解時,有一個小的差異。 – sandeepsure

1

代替下面線

$sorted = val_sort($allplayers, '[season]'); 

你應該通過重點如下

$sorted = val_sort($allplayers, 'season'); 

希望這有助於。

0

我在這種情況下做了躲閃速戰速決。 我寫了一個循環到array_pop主數組內的每個數組,並且爲array_pop循環進行時爲每個數據段創建了一個新變量。 我也array_push編輯我想排序的數據和ID號到一個新的數組。 我用rsort對新數組進行排序,然後使用ID號碼循環遍歷它,以按照我想輸出數據的順序重建第三個數組。 下一次我會接受@rocky的建議並做一個數據表。我甚至可以回去重做這個頁面作爲數據表。說實話,我昨天沒有聽說過數據表。一旦我將這個項目交給老闆,我將會花時間學習數據表和AJAX。 謝謝大家的提示,但快速解決方案將不得不這樣做,因爲我時間很窮(這個網站需要在明天上午生活)。