Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP哈希按值组合组数组_Php_Arrays_Cakephp_Grouping - Fatal编程技术网

CakePHP哈希按值组合组数组

CakePHP哈希按值组合组数组,php,arrays,cakephp,grouping,Php,Arrays,Cakephp,Grouping,我有一个关联的数据数组,需要从中使用关联数组值进行分组 $data = Array ( [0] => Array ( [App] => Array ( [id] => 12 [user_id] => 121 [skill] => Baking Cakes ) [Project] => Array

我有一个关联的数据数组,需要从中使用关联数组值进行分组

$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
        )
    )
)
是否可以使用哈希实用程序使用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,但都没有实现。 有人能有个主意吗


提前谢谢

以下行将生成一个与您所需类似的数组:

$result=Hash::combine($data, '{n}.App.id', '{n}','{n}.User.id');
除了嵌套数字索引保存存储在
App.id
中的数字,而不是顺序数字索引

如果
App.id
在您的结果集中是唯一的,这将起作用。或者,您可以使用
Project.id
作为
$keyPath

参考:


要改进@Inigo Flores答案,您可以通过将第二个参数设置为null来索引而不是关联:

$result=Hash::combine($data, null, '{n}','{n}.User.id');

您可以使用
foreach
循环创建此数组。您只需一步-如何使此数组使用索引而不是关联?