如何使用 PHP 将具有匹配列值的 MySQL 查询结果放入基于该值的关联数组中?
我有一个具有垂直模式的数据库,解释为:
Key、Cat、UID、Var、Val 等... 其中 Key 是索引,Cat 是类别,UID 是一组行的标识符,var 是该行代表的值的名称,Val 是该值。因此,几行看起来像这样:
key cat uid var val
1 1 98765 name David
2 1 98765 description handsome, young, sporting,
3 1 98765 phone 123-456-789
4 1 98765 email [email protected]
5 1 12345 name Jason
6 1 12345 description cool, hot, tall,
7 1 12345 phone 222-555-1244
8 1 12345 email [email protected]
8 1 12345 website www.jason.com
因此,query/php 的目标是从指定类别中获取所有行,然后根据具有该 uid 的所有行的 uid 返回一个关联数组。结果会是这样的
results{
["98765"] => ["name"] = David
=> ["description"] = handsome, young, sporting,
=> ["phone"] = 123-456-789
=> ["email"] = [email protected]
["12345"] => ["name"] = Jason
=> ["description"] = cool, hot, tall,
=> ["phone"] = 222-555-1244
=> ["email"] = [email protected]
=> ["website"] = www.jason.com
}
希望这是可以理解的,如果我的问题不清楚,请告诉我。
I have a database with a vertical schema, explained:
Key, Cat, UID, Var, Val, etc...
Where Key is the index, Cat is the category, UID is the identifier for a group of rows, var is the name of the value the row represents and Val is that value. So a few rows would look something like this:
key cat uid var val
1 1 98765 name David
2 1 98765 description handsome, young, sporting,
3 1 98765 phone 123-456-789
4 1 98765 email [email protected]
5 1 12345 name Jason
6 1 12345 description cool, hot, tall,
7 1 12345 phone 222-555-1244
8 1 12345 email [email protected]
8 1 12345 website www.jason.com
So the goal with the query/php is to get all rows from a specified category, then return an associative array based on the uid for all the rows with that uid. The result would be somehting like
results{
["98765"] => ["name"] = David
=> ["description"] = handsome, young, sporting,
=> ["phone"] = 123-456-789
=> ["email"] = [email protected]
["12345"] => ["name"] = Jason
=> ["description"] = cool, hot, tall,
=> ["phone"] = 222-555-1244
=> ["email"] = [email protected]
=> ["website"] = www.jason.com
}
Hopefully that's understandable, let me know if my question is unclear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的事情:
Try something like this:
如果这样做的原因是使用 result["98765"] 类型语法访问结果,则最好使用对象。您可以引用对象或使用对象数组,但代码看起来会干净得多
if the reason for doing this is to access the results using result["98765"] type syntax you may be better using objects. you could reference the objects or use an array of objects but the code would look a lot cleaner