这里是新的,但喜欢阅读别人的问题和答案。我对PHP相当陌生,正在开发一个项目 - 在MySQL中查找基本表格等。 我最终想要的是一个数组数组,其形式如下所示(不是我的实际项目)。内容来自两个不同的表格。对于每个调味品名称(来自表1),我在表2中查找通过ID链接的调味品的类型。搜索和抓取的东西很好,但我无法循环和建立我最后的$ Condiments数组。从sql结果数组中迭代附加数组
循环的第一部分,我从$行中获取调味品名称并将其追加到数组中。但我需要这些调味品名称中的每一个都是一个空数组,以便在下一步中放入某些东西。我环顾四周,但无法找到一个好的方法来迭代地将新的占位符数组附加到数组中。有没有优雅的解决方案?一些很酷的功能,我没有利用?谢谢!
// SQL search for condiment words, blah blah, leading to...
$rowsnumber = mysql_num_rows($result);
for ($j = 0 ; $j < $rowsnumber ; ++$j)
{
$row = mysql_fetch_row($result); // $row is an array featuring a condiment name and other stuff.
$Condiments[] = $row[1]; // condiment name goes in array.
$CondimentType = searchTable2($row[0]);
// using the condiment name's ID, I look up its matching types via a function.
// $CondimentType is now an array of IDs and types from Table2 that I want to append to the condiment name I just added above.
$Condiments[$row[1]] = $CondimentType;
// I repeat the process for the next name
}
// Final desired result...
$Condiments=
Array
(
[Pickles] => Array
(
[34] => Dill
[23] => Butter
)
[Mustard] => Array
(
[22] => Hot
)
[Relish] => Array
(
[3] => Pickle
)
)
如果你可以发布你的数据库的模式,它会帮助我们很多。从我所了解的情况来看,你似乎需要加入这两张表来加速这个过程 –
@Don接近我的真实项目。基本上是两个表格,一个是文字,另一个是将单词连接在一起。一位朋友还建议使用SQL来合并表格信息。不知道如何在评论字段中更好地显示。 'INSERT INTO表1(ID,字)参数值 ( '25', '酱菜'), ( '74', '芥末'), ( '85', '津津乐道'), ( '34' ,'Dill'), ('23','Butter'), ('22','Hot'), ('3','Pickle'); INSERT INTO表2(ID,链路ID)VALUES ( '25', '34'), ( '25', '23'), ( '74', '22'), ( '85', '3');' –