如何使用 PHP 将具有匹配列值的 MySQL 查询结果放入基于该值的关联数组中?

发布于 2024-12-16 12:27:32 字数 1596 浏览 0 评论 0原文

我有一个具有垂直模式的数据库,解释为:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回首观望 2024-12-23 12:27:32

尝试这样的事情:

$category = 1;
$result = mysql_query( 'SELECT * FROM table WHERE cat = ' . intval( $category));
$results = array();
while( $row = mysql_fetch_array( $result))
{
    $results[ $row['uid'] ][ $row['var'] ] = $row['val'];
}
mysql_free_result( $result);

Try something like this:

$category = 1;
$result = mysql_query( 'SELECT * FROM table WHERE cat = ' . intval( $category));
$results = array();
while( $row = mysql_fetch_array( $result))
{
    $results[ $row['uid'] ][ $row['var'] ] = $row['val'];
}
mysql_free_result( $result);
梦亿 2024-12-23 12:27:32

如果这样做的原因是使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文