2012-02-22 11 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>"; 

ループ(ですなぜ、例えば、その後、$increment2 = 2 場合、誰かが私に説明でき

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に格納されて

:ここでは)は、配列$gamesがどのように見えることができるものの一例です$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

ありがとう、リカルドと@ニーソー! 私はいくつかの変更を行い、それは働いた: 最初にあなたが示唆したようにnpcommidを引用し、その後、$ increment2でインクリメントする$記号を追加しました。それはまだ動作しませんでしたが、 私はループ内の変数を置く場合、それは実際には変数が実際にループを行ったと言われ、それが最終的に表示されるように考えた:D クエンティン、私は申し訳ありません今日だけこのコミュニティにサインアップしました。私はまだすべてを学んでいますが、編集方法について教えてくれてありがとうございます:) 申し訳ありません、私は投票できません:( –

1

あなたはいつもerror_reporting(E_ALL);はあなたが持っていたエラーを知るために追加することができ、同じプロジェクトにコードをフォーマットしてください

関連する問題