2012-02-22 5 views
0

변수를 사용하여 배열 이름이나 키를 지정하는 방법에 대한 도움이 필요합니까? 내가 셀 수없이 많은 시간을 시도하고 내가 코드에 도착하기 전에 그것이 결코PHP에서 변수를 사용하여 배열을 지정하는 방법은 무엇입니까?

작동하지 $ 게임을 배열하고, 내가 여기 바닥

에서 보여 것은 (전에서 배열의 예를 보여줍니다 내 코드입니다 하단) // 여기

$increment = 0; //increment i use in a while loop +1 per loop starting at 0 
$increment2 = increment - 1; // to get the game number i need to minus 1 from $increment, 
//this works since the array starts at 0 and increment will be 1 when the while loop starts. 

$gamestotal = $games[totalGames]; //This number is different everytime, it is always 
//between 1-1000, it counts the number of a games a person has. 
$gamesnpcommid = $games[$increment2][npcommid]; //each game has a unique code, i cURL this 
//info from Sony servers and return it in an array (array example below) This is the part 
//that is not working, to help understand please check array first, this variable is used 
//in the while loop and i need it to start at array 0 and keep returning the unique code 
//until the loops stops at the final array number. 

while($increment<$gamestotal) 

    if($increment % 2 == 0){ 
    $increment=$increment+1; 
     echo "<tr>"; 
     echo "<td style='background-color:#e6e8fa'>"; 
     echo $gamesnpcommid; 
     echo "</td>"; 
     echo "</tr>"; 
    } 
    else 
    {$increment=$increment+1; 
     echo "<tr>"; 
     echo "<td style='background-color:adeaea'>"; 
     echo $gamesnpcommid; 
     echo "</td>"; 
     echo "</tr>"; 
    } 

echo "</table>"; 

모두 완전히 잘 작동하지만, 루프 배열 참조가 작동하지 않는 (인 210,403,210)은, 여기에 배열 $games이 어떻게 할 수 있는지의 예입니다

Array 
(
    [totalGames] => 110 
    [0] => Array 
     (
      [npcommid] => NPWR00507_00 
      [platinum] => 0 
      [gold] => 1 
      [silver] => 11 
      [bronze] => 32 
      [total] => 44 
      [lastupdated] => 1329258539 
     ) 

    [1] => Array 
     (
      [npcommid] => NPWR01140_00 
      [platinum] => 1 
      [gold] => 4 
      [silver] => 8 
      [bronze] => 29 
      [total] => 42 
      [lastupdated] => 1328586022 
     ) 

    [2] => Array 
     (
      [npcommid] => NPWR01118_00 
      [platinum] => 0 
      [gold] => 0 
      [silver] => 3 
      [bronze] => 8 
      [total] => 11 
      [lastupdated] => 1328067349 
     ) 
    ) 

누군가가 나에게 설명 할 수,

배열 변수 $games에 저장하는 이유, 예를 들어, $increment2 = 2 다음 $games[$increment2][npcommid]을 사용하여 배열을 참조하려고하면 배열에서 NPWR01118_00을 반환하지 않습니까? 그러나 $games[2][npcommid]을 사용할 때 작동합니까? 철저한 디버깅 작업을 수행했으며 배열 작동을 알았지 만이 시점에서 난처한 상황입니다.

+0

지금 일하고 있습니다. 입력란 위의 주황색 물음표 아이콘을 클릭하면 안내가 나타납니다. 나는 지금 당신이 가진 것을 읽으 려하지 않을 것입니다. – Quentin

+1

increment2는 -1이며 코드에서 변경되지 않습니다 ... 코드에서 사용자의 위치가 변경되지 않았습니까? – Neysor

+0

대신이 배열을 반복하기 위해'foreach' 루프를 사용하지 않으시겠습니까? – Svish

답변

0

$games[$increment2]["npcommid"] 

대신 즉

$games[$increment2][npcommid] 

시도 연관 키는 항상 인용되어야한다.

+0

문서에서 인용하기 "PHP가 맨손으로 된 문자열 (알려진 기호와 일치하지 않는 인용되지 않은 문자열)을 맨 문자열을 포함하는 문자열로 자동 변환하기 때문에 작동합니다." 따라서 여기에는 해당되지 않지만, 그렇습니다. 어쨌든 해결해야합니다. – Furgas

+0

감사합니다. Ricardo와 @Neysor! 약간의 변경을했고 효과가있었습니다 : 처음에 npcommid를 제안한 것처럼 인용 한 다음, Neysor가 언급 한 것을 잊어 버린 이후 $ increment2로 증가시키기 위해 $ 기호를 추가했습니다. 그것은 아직 작동하지 않았지만, 나는 변수가 실제로 루프를 할 때마다 언급하고 그것이 마침내 표시되는 루프 내에서 변수를 넣어 계산 : D Quentin, 미안, 나 방금 오늘이 커뮤니티에 가입했고 아직 모든 것을 배웠습니다. 편집하는 방법을 알려 주셔서 감사합니다. :) 죄송합니다. 투표 할 수 없습니다. –

1

당신은 항상 error_reporting(E_ALL); 당신이 있던 오류를 알고 추가 할 수 있습니다, 같은 프로젝트 코드를 포맷하십시오

관련 문제