Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中使用变量指定数组?_Php_Arrays_Variables_Key - Fatal编程技术网

如何在php中使用变量指定数组?

如何在php中使用变量指定数组?,php,arrays,variables,key,Php,Arrays,Variables,Key,我需要关于如何使用变量指定数组名或键的帮助? 我试过无数次,但都没用 在我开始编写代码之前,$games是数组,我在底部显示它 $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

我需要关于如何使用变量指定数组名或键的帮助? 我试过无数次,但都没用

在我开始编写代码之前,$games是数组,我在底部显示它

$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
然后,当我尝试使用
$games[$increment2][npcommid]
引用数组时,它不会像数组中那样返回NPWR01118_00? 但是当我使用
$games[2][npcommid]
时,它能工作吗? 我已经完成了彻底的调试,我知道阵列可以正常工作,但我现在很为难。

试试看

$games[$increment2]["npcommid"] 
而不是

$games[$increment2][npcommid]

换句话说,必须始终引用关联键。

您始终可以添加
错误报告(E\u ALL)要知道您的错误,我现在正在处理同一个项目

请格式化您的代码。单击编辑框上方的橙色问号图标将为您提供说明。我甚至不打算读你现在得到的东西。increment2是-1,在你的代码中从未更改过。。。因此,它在代码中的位置可能没有改变?为什么不使用
foreach
循环来循环此数组?修复第二行
$increment2=$increment-1(缺少$)并检查结果。引用文档“这是因为PHP会自动将空字符串(与任何已知符号都不对应的未加引号的字符串)转换为包含空字符串的字符串。”。所以这里的情况不是这样,但是的,无论如何都应该修复。非常感谢里卡多和@Neysor!我做了一些修改,效果很好:首先我引用了你建议的npcommid,然后我在$increment2中添加了$increment符号,因为我忘记了Neysor提到的这一点。虽然它还不起作用,但我想如果我把变量放在循环中,它可能会起作用,所以每次循环时都会提到这个变量,最后它会显示:D Quentin,对不起,我今天刚注册到这个社区,我还没有学到所有东西,感谢您让我知道如何编辑:)抱歉,我不能投票支持:(
$games[$increment2][npcommid]