使用 sql 结果中的数组迭代追加数组
我是新来的,但很喜欢阅读其他人的问题和答案。我对 PHP 相当陌生,正在开发一个项目 - MySQL 中的基本表查找等。 我想要最终得到的是一个数组的数组,其形式如下所示,带有调味品(不是我的实际项目)。内容来自两个不同的表。对于每种调味品名称(来自表 1),我在表 2 中查找调味品的类型(通过 ID 链接)。搜索和抓取内容很好,但我在循环和构建最终的 $Condiments 数组时遇到问题。
循环的第一部分,我从 $row 中获取调味品名称并将其附加到数组中。但我需要每个调味品名称都是一个空数组,以便在下一步中放入一些内容。我环顾四周,但找不到一种好方法来迭代地将新的占位符数组追加到数组中。有优雅的解决方案吗?有些很酷的功能我没有利用?谢谢!
// 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
)
)
am new here, but have enjoyed reading others' questions and answers. I'm fairly new to PHP and am working on a project - basic table look-ups in MySQL, etc.
What I want to end up with is an array of arrays, the form of which is shown below with condiments (not my actual project). The content is coming from two different tables. For each of the condiment names (from table 1) I look up in table 2 the types of condiments, linked by ID. The searching and grabbing stuff is fine, but I'm having trouble looping and building my final $Condiments array.
The first part of the loop, I grab the condiment name from the $row and append it to the array. But I need each of these condiment names to be an empty array to put something in, in the next step. I've looked around but couldn't find a good way to iteratively append new placeholder arrays into an array. Is there an elegant solution? Some cool function I'm not taking advantage of? Thanks!
// 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
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我所说,您需要使用 join 来执行所需的任务。
您可以在这里找到有关连接的更多说明
http://dev.mysql.com /doc/refman/5.0/en/join.html
在你的情况下,这个查询应该完成我在我的机器中运行查询的工作
,这些是结果
,所以不需要循环每一行,只需执行一个连接并得到结果。在 php 中,您可以执行所需的数组格式。
希望这会帮助你
so like i said , you need to use join to perform the needed task.
you can find more explanation here about joins
http://dev.mysql.com/doc/refman/5.0/en/join.html
in your case , this query should do the job
i ran the query in my machine and these are the results
so instead of looping through each row, just perform a join and get the results. in php then you can do the needed format of the array.
hopefully this will help you