如何在 PHP 中将此 MN mysql 数组转换为另一个数组? (里面有例子)
好吧,在 mysql 的关系 MN 中获得这样的数组是很常见的:
[0]
'book_id' => 1
'title' => 'title'
'author_id' => 1
'author_name' => 'name1'
[1]
'book_id' => 1
'title' => 'title'
'author_id' => 2
'author_name' => 'name2'
那么,有一种优雅/简单的方法来转换这种类型或类似的数组吗?
'book_id' => 1
'title' => 'title'
'authors' => array( 0 => array( 'author_id' => 1, 'author_name' => 'name1' ),
1 => array( 'author_id' => 2, 'author_name' => 'name2' ) )
我没有找到任何脚本或函数组合来实现这个...并且很常见,也许我没有正确搜索..不知道如何调用问题...
有什么想法或经验吗?
谢谢:)
PS:我不想在 MySQL 中使用 GROUP BY + GROUP CONCACT,我发现这个解决方案非常丑陋......
编辑: 我正在做一些通用的事情,不仅仅是为了解决这个特定的问题..
Well, is pretty common to get an array like this in relations M-N in mysql:
[0]
'book_id' => 1
'title' => 'title'
'author_id' => 1
'author_name' => 'name1'
[1]
'book_id' => 1
'title' => 'title'
'author_id' => 2
'author_name' => 'name2'
So, there is an elegant/easy way to convert this kind or arrays in something like this?
'book_id' => 1
'title' => 'title'
'authors' => array( 0 => array( 'author_id' => 1, 'author_name' => 'name1' ),
1 => array( 'author_id' => 2, 'author_name' => 'name2' ) )
I don't find any script or combinations of functions that made this... and is pretty common, maybe I have not searched correctly.. don't know how to call the problem...
Any ideas or experiences?
Thanks :)
PS: I don't want to use GROUP BY + GROUP CONCACT in MySQL, I found that pretty ugly solution...
EDIT:
I'm working in something generic, not only to solve this specific problem..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会在 SQL 中执行 JOIN 操作,然后将结果后处理到 PHP 代码中的嵌套数组中。
如果您希望最终的数组是一个序数数组而不是由 book_id 作为键,您可以将其转换:
I would do the JOIN in SQL and the post-process the results into a nested array in PHP code.
If you want the final array to be an ordinal array instead of keyed by book_id, you can convert it:
多个子键的通用解决方案:
这使您可以传递主键以及子键和字段的哈希值以进行聚合。
A generic solution for multiple sub-keys:
This lets you pass the primary key and a hash of sub keys and fields to aggregate by.
好吧,我有一个基于 Bill Karwin 建议的“解决方案”:
不知道是否是最有效/最正确的。另一方面,如果存在不止一种类型的冲突,这将不起作用...补丁将是好评:)
谢谢大家!
Well, I have a "solution" based in Bill Karwin suggestion:
Don't know if is the most eficient/correct.. On other hand, this will not work if there are more than one type of conflict... patches will be good received :)
thanks to all!
像这样简单的东西就可以工作:
行由书籍 ID 和作者 ID 键入,因此每本书只有一个输出行,然后该行中的每个作者都有一个输出行。
Something simple like this would work:
Rows are keyed by book ID and then author ID so we only have one output row per book and then per author inside that row.
好吧,这是我最终将使用的函数,它完全基于 Cal 的想法,但尝试使用本机函数,如 Bill Karvin 所说:
}
一旦完成,我认为它不会有太多好的性能。 .我想知道其他人如何解决此类问题...
无论如何,感谢两者!
Well, this is the funcion that finally I will use, it is based completly in the idea of Cal but trying to use native functions as said Bill Karvin:
}
Once done, I don't think it has a too much good performance... I wonder how other people solve this kind of problems...
Whatever, thanks to both!