【问题标题】:How to specify an array using a variable in php?如何在 php 中使用变量指定数组?
【发布时间】:2012-02-22 12:06:34
【问题描述】:

我需要有关如何使用变量指定数组名称或键的帮助? 试了无数次都没成功

在我进入代码之前,$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>";

一切正常,但数组引用不起作用 ($gamesnpcommid),下面是数组 $games 的示例:

数组存储在变量$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
        )
    )

谁能给我解释一下为什么,例如,如果$increment2 = 2 然后当我尝试使用$games[$increment2][npcommid] 引用数组时,它不会像数组中那样返回 NPWR01118_00? 但是当我使用$games[2][npcommid] 时它可以工作吗? 我已经完成了彻底的调试,我知道数组可以工作,但我现在很难过。

【问题讨论】:

  • 请格式化您的代码。单击编辑框上方的橙色问号图标将为您提供说明。我什至不会尝试阅读您现在所拥有的内容。
  • increment2 是 -1 并且从未在您的代码中更改过......所以也许它在您在代码中的位置没有更改?
  • 为什么不使用foreach 循环来循环这个数组?
  • 修正第二行$increment2 = $increment - 1;(缺少$)并检查结果。

标签: php arrays variables key


【解决方案1】:

您可以随时添加error_reporting(E_ALL); 以了解您遇到了哪个错误,正在处理同一个项目

【讨论】:

    【解决方案2】:

    试试

    $games[$increment2]["npcommid"] 
    

    而不是

    $games[$increment2][npcommid]
    

    换句话说,关联键总是必须被引用。

    【讨论】:

    • 从文档中引用“它之所以有效,是因为 PHP 会自动将裸字符串(与任何已知符号不对应的未加引号的字符串)转换为包含裸字符串的字符串。”。所以这里不是这样,但是是的,无论如何都应该修复它。
    • 非常感谢 Ricardo 和 @Neysor!我做了一些更改并且它起作用了:首先我像你建议的那样引用了 npcommid,然后我添加了 $ 符号以在 $increment2 中递增,因为我忘记了 Neysor 提到的这一点。虽然它还没有工作,但我想如果我将变量放在循环中它可能会工作,所以每次它执行循环时实际上都会提到变量并且它最终显示:D Quentin,对不起,我刚刚注册今天来到这个社区,我还没有学到所有东西,谢谢你让我知道如何编辑:)对不起,我不能投票:(
    猜你喜欢
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    相关资源
    最近更新 更多