简体   繁体   English

PHP多维数组帮助

[英]PHP Multi Dimensional Array Help

I am populating an array from a MySQL Database, the problem is I get 9 blank fields in my dropdown box that I am trying to populate it with. 我正在从MySQL数据库填充数组,问题是我在下拉列表中尝试填充9个空白字段。 What did I do wrong? 我做错了什么?

php code: php代码:

while($row = mysql_fetch_assoc($results))
{
    $options[] = array(
                        Name => $row['Name'], 
                        Value => $row['Value'], 
                        ID => $row['CID']
                      );
}

dropdown box: 下拉框:

for ($i = 0; $i < 9; $i++)
{
    print '<option id="'.$options[$i]["ID"].'" value="'.$options[$i]["Value"].'">'
          .$options[$i]["Name"].
          '</option>'."\n";
}

Your quotes are backwards in your array 您的报价在数组中是向后的

while($row = mysql_fetch_assoc($results))
{
    $options[] = array("Name" => $row['Name'], "Value" => $row['Value'], "ID" => $row['CID']);
}

Also, using an associative array in double quotes requires you to do this: 另外,在双引号中使用关联数组要求您执行以下操作:

echo "I want to print out this: {$arr['index']}";

If you don't do it this way, it (should) result in a parse error. 如果您不这样做,则(应该)会导致解析错误。

You might notice that this also works 您可能会注意到这也有效

echo $arr[index]

But this is not good practice because if there is a constant through a define() named index, all of your statements will use your constant's definition instead of the string you intended. 但这不是一个好习惯,因为如果通过名为index的define()有一个常量,则所有语句将使用该常量的定义而不是您想要的字符串。

Assuming your query actually executes, the problem is with how you're initializing your array values to your array keys. 假设您的查询实际执行,问题在于如何将数组值初始化为数组键。 In PHP, the keys of an associative array must be strings, and strings in PHP are quoted with single or double quotes. 在PHP中,关联数组的键必须是字符串,并且PHP中的字符串用单引号或双引号引起来。

 $foo = array(Name => 'value');   // incorrect, Name isn't a string
 $foo = array('Name' => 'value'); // correct

Accessing associative arrays also requires using strings for keys: 访问关联数组还需要使用字符串作为键:

 $foo[Name]   // incorrect, Name isn't a string
 $foo['Name'] // correct

When using arrays inside double quoted strings, to get their value you should surround the arrays with braces: 在双引号字符串内使用数组时,要获取其值,应在括号周围加上数组:

 "$foo['Name']"    // can cause problems
 "{$foo['Name']}"  // good practice

Note you can use the technique for braces with regular variables: 请注意,您可以将该技术用于带有常规变量的括号:

 $bar = "Hello";
 $foo['Name'] = "World";
 echo "{$bar} {$foo['Name']}"; // prints Hello World!

If all you want to do is save all your rows, you can just store the whole $row array: 如果您要做的只是保存所有行,则可以存储整个$row数组:

$options[] = $row;

rather than copying each individual value from array to array. 而不是将每个单独的值从一个数组复制到另一个数组。 This is more concise, more readable, less error prone, and will generalize if the contents of $row changes. 这更加简洁,可读性强,更不易出错,并且如果$row的内容发生更改,它将得到概括。 Of course, if the content of $row changes, this has the pitfall of requiring changes to things that depend on $options . 当然,如果$row的内容发生更改,则存在必须更改依赖于$options事物的陷阱。

You must quote the array keys Name, Value, ID : 您必须引用数组键Name, Value, ID

 $options[] = array('Name' => "$row['Name']", 'Value' => "$row['Value']", 'ID' => "$row['CID']");

Placing quotes around the "$row['key']" is unnecessary and redundant. "$row['key']"周围放置引号是不必要的,也是多余的。

The PHP array documentation PHP数组文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM