【发布时间】:2014-11-03 10:14:20
【问题描述】:
我正在尝试将 Drupal 数据库中的关联数组转换为可以编码为 json 的多维数组。
我开始:
$notifications =`
Array
(
[0] => Array
(
[rfp_id] => RFP-013-2014(C)
[notification_type] => due_date
)
[1] => Array
(
[rfp_id] => RFP-013-2014(C)
[notification_type] => changes
)
[2] => Array
(
[rfp_id] => RFP-013-2014(C)
[notification_type] => due_date
)
[3] => Array
(
[rfp_id] => RFP-014-2014(C)
[notification_type] => due_date
)
[4] => Array
(
[rfp_id] => RFP-014-2014(C)
[notification_type] => changes
)
)
我想按 rfp_id 字段分组并最终得到如下内容:
Array (
[0]=> Array (
["rfp_id"]=>"RFP-014-2014"
["notification_type"]=>
Array (
[0]=> "date_due"
[1]=> "changes"
)
)
)
我将如何遍历这个数组来创建它?
【问题讨论】:
-
您为此做了哪些努力?
-
@MikeBrant 我尝试了几个不同的选项来循环遍历数组。我得到的最接近的是每个 notification_type 作为一个数组,而不是所有 notification_type 作为一个数组。
-
@CharlotteDunois 不,它不一样。我需要按其中一个值分组。
标签: php arrays multidimensional-array