我有一个关联的数据数组,需要使用关联数组值进行分组。
$data = Array
(
[0] => Array
(
[App] => Array
(
[id] => 12
[user_id] => 121
[skill] => Baking Cakes
)
[Project] => Array
(
[id] => 12
[name] => P1
)
[User] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
),
[1] => Array
(
[App] => Array
(
[id] => 15
[user_id] => 121
[skill] => Baking Cakes
)
[Project] => Array
(
[id] => 13
[name] => P2
)
[User] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
)
)
是否可以使用Hash实用程序使用User.id对数组进行分组? 像这样:
Array
(
[121] = Array
(
[0] => Array
(
[App] => Array
(
[id] => 12
[user_id] => 121
[skill] => Baking Cakes
)
[Project] => Array
(
[id] => 12
[name] => P1
)
[User] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
),
[1] => Array
(
[App] => Array
(
[id] => 15
[user_id] => 121
[skill] => Baking Cakes
)
[Project] => Array
(
[id] => 13
[name] => P2
)
[User] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
)
)
)
我尝试了几个方法Hash :: combine(),Hash :: extract但是无法实现它。 有人可以有一个想法。
提前致谢!
答案 0 :(得分:0)
以下行将生成一个类似于您需要的数组:
$result=Hash::combine($data, '{n}.App.id', '{n}','{n}.User.id');
除了嵌套数字索引保存App.id
中存储的数字而不是顺序数字索引。
如果App.id
在结果集中是唯一的,那么这将有效。或者,您可以将Project.id
用作$keyPath
。
参考:
答案 1 :(得分:0)
要改善@Inigo Flores的答案,可以通过将第二个参数设置为null来获得索引而不是关联的索引:
$result=Hash::combine($data, null, '{n}','{n}.User.id');