双美元符号 - 分配数组 key=>value
我想创建 3 个不同的数组,它们可以根据从某些 mysql 表中提取的字段而变化。
这是我的代码:
// Gather db rows with query
$compQuery['subCats'] = mysql_query("SELECT id,name,category FROM subcategories ORDER BY id DESC");
$compQuery['cats'] = mysql_query("SELECT id,name FROM categories ORDER BY id DESC");
$compQuery['mans'] = mysql_query("SELECT id,name,link FROM manufacturers ORDER BY id DESC");
// Place rows into arrays
foreach($compQuery as $name=>$query){
$name = array();
while($data = mysql_fetch_array($compQuery[$name])){
foreach($data as $key=>$value){
$$name[$key] = $value;
}
}
}
我希望它生成 3 个数组:subCats、cats 和 mans,每个数组都有值对。它不是查找 $$name 变量并将 [$key] = $value 分配给数组,而是从 $$name 变量中取出一个字母并将 $value 分配给这个新变量。
例如,如果 $name = 'subCats',$key = '3',并且 value = 'machinetools'
它将使用 subCats 中的第三个字母创建变量 $C:
$C = 'machine tools'
它应该做的是创建:
$subCats[3] = 'machine tools';
现在,显然有是为每个查询创建单独数组的更简单方法,并且我已将代码修改为更懒/更简单的版本。但是,我仍然很好奇如何将键值对分配给双美元符号变量。
任何帮助将不胜感激!我现在可以使用简单的代码,但希望实现这个新代码,使其更加动态,并允许根据需要进行更多查询,而无需麻烦。
谢谢!
乔什
I would like to create 3 different arrays which can change depending on which fields are pulled from some mysql tables.
This is the code I have:
// Gather db rows with query
$compQuery['subCats'] = mysql_query("SELECT id,name,category FROM subcategories ORDER BY id DESC");
$compQuery['cats'] = mysql_query("SELECT id,name FROM categories ORDER BY id DESC");
$compQuery['mans'] = mysql_query("SELECT id,name,link FROM manufacturers ORDER BY id DESC");
// Place rows into arrays
foreach($compQuery as $name=>$query){
$name = array();
while($data = mysql_fetch_array($compQuery[$name])){
foreach($data as $key=>$value){
$name[$key] = $value;
}
}
}
What I would expect this to produce is 3 arrays: subCats, cats and mans each with the value pairs. Instead of finding the $$name variable and assigning [$key] = $value to the array it is taking a single letter out of the $$name variable and assigning the $value to this new variable.
For example, if $name = 'subCats', $key = '3', and value = 'machine tools'
It would create the variable $C using the 3rd letter in subCats:
$C = 'machine tools'
What it should do is create:
$subCats[3] = 'machine tools';
Now, obviously there are easier ways to create separate arrays for each query, and I have modified my code to a lazier/easier version. However, I am still curious as to how I would go about assigning a key value pair to a double dollar sign variable.
Any help would be greatly appreciated! I have the simple code working now, but would like to implement this new code to be more dynamic and allow for more queries as needed without hassle.
Thanks!
Josh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
${$name}[$key]
但我不喜欢创建“未知”变量,因为名称不在代码中的变量...如果名称是“key”怎么办, 'db'、'_GET'、'compQuery' 等?一切地狱都崩溃了……
${$name}[$key]
But I do not like to create 'unknown' variable, as in variables whose name is not in the code... What if the name is 'key','db','_GET','compQuery' etc? All hell breaks loose...
在这一行中:
您将覆盖
$name
。您可能想写信给$$name
:In this line:
You're overwriting
$name
. You probably meant to write to$$name
: