Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.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
在PHP上重新排列关联数组_Php_Arrays - Fatal编程技术网

在PHP上重新排列关联数组

在PHP上重新排列关联数组,php,arrays,Php,Arrays,在我写了这个问题之后,我意识到代码太多了,但这主要是因为我想让你理解我需要什么,让你能够复制并修复这个代码。我觉得anwser可能有点简单 我编写了一个函数来创建关联数组,但在获取所需的结构时遇到了问题。因此,为了让我的问题更清楚,我将发布复制输出所需的所有代码,在我这样做的同时,我将尝试用内部的coments解释问题 这是我的功能: foreach ($developers as $key => $value) { for ($h=0; $h < count($data["todo

在我写了这个问题之后,我意识到代码太多了,但这主要是因为我想让你理解我需要什么,让你能够复制并修复这个代码。我觉得anwser可能有点简单

我编写了一个函数来创建关联数组,但在获取所需的结构时遇到了问题。因此,为了让我的问题更清楚,我将发布复制输出所需的所有代码,在我这样做的同时,我将尝试用内部的coments解释问题

这是我的功能:

foreach ($developers as $key => $value) {
for ($h=0; $h < count($data["todo-items"]); $h++) {
    if ($value == $data["todo-items"][$h]['responsible-party-id']) {
        $todayArray[$value]["id"] = $value;
        $todayArray[$value]["responsibleName"] [] = $data["todo-items"][$h]['responsible-party-names'];
        $todayArray[$value]["responsibleName"] ["project"][] = $data["todo-items"][$h]['project-id'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["id"] = $data["todo-items"][$h]['id'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["content"] []= $data["todo-items"][$h]['content'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["progress"] []= $data["todo-items"][$h]['progress'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["completed"][] = $data["todo-items"][$h]['completed'];
    }
  }
}
Array
(
[159667] => Array //responsible ID as key 
    (
        [id] => 159667 //responsible ID
        [responsibleName] => Array
            (
                [0] => Someone N. // This is stored as an array but it will only contain this same name in all positions as we are inside his id
                [project] => Array
                    (
                        [0] => 273774 // ID of the project (maybe tasks from different projects here)
                        [task] => Array //This is the structure that I want to repeat the most (one per task) 
                            (
                                [id] => 12046737 // ID of the task
                                [content] => Array
                                    (
                                        [0] => TST - Task1
                                        [1] => TST - Task2 //This don't belong in here, it should be under another task id
                                )
                                    )

                                [progress] => Array
                                    (
                                        [0] => 30
                                        [1] => 30 //This don't belong in here it should be under another task id
                                    )

                                [completed] => Array
                                    (
                                        [0] => false
                                        [1] => false //This don't belong in here it should be under another task id
                                    )

                            )

                        [1] => 273775 // The second tasks it's from a different project so it should be printed here or if was from the same project above this. 
                    )

                [1] => Someone N. // The task is assigned to the same person (because of the if condition) so this shouldn't be needed
        )
            )

    )

   //The next structure maybe ok, but it only works if the responsible has only one task assigned, and still has too many arrays inside it I guess

[123983] => Array
    (
        [id] => 123983
        [responsibleName] => Array
            (
                [0] => Someone Else
                [project] => Array
                    (
                        [0] => 723764
                        [task] => Array
                            (
                                [id] => 12046739
                                [content] => Array
                                    (
                                        [0] => TST - Task3
                                    )

                                [progress] => Array
                                    (
                                        [0] => 100
                                    )

                                [completed] => Array
                                    (
                                        [0] => true
                                    )

                            )

                    )

            )

    )

)
    Array
      (
      [159667] => Array
        (
            [id] => 159667
            [responsibleName] => Someone N.
                    [project] => Array
                        (
                            [0] => 273774
                            [task] => Array
                                (
                                    [id] => 12046737
                                    [content] =>  TST - Task1
                                    [progress] => 30
                                    [completed] => false
                                )
                             [1] => 273775
                             [task] => Array
                                 (
                                     [id] => 12046738
                                     [content] =>  TST - Task2
                                     [progress] => 30
                                     [completed] => false
                                 )

                        )
        )        
    [123983] => Array
      (
          [id] => 123983
          [responsibleName] => Someone Else
                  [project] => Array
                      (
                           [0] => 723764
                          [task] => Array
                             (
                                [id] => 12046739
                                [content] =>  TST - Task3
                                [progress] => 100
                                [completed] => true
                              )
                      )
      )
)
$developers= array(
 "0"=>  "163289",
 "1"=>  "159667",
 "2"=>  "91889",
 "3"=>  "123983"
);

$data =  array(
 "todo-items" => array(
            "0" => array(
                "id" => "12046737",
                "content" => "TST - Task1",
                "project-id" => "273774",
                "completed" => "false",
                "progress" => "30",
                "responsible-party-id" => "159667",
                "responsible-party-names" => "Someone N."
            ),
            "1" => array(
                "id" => "12046738",
                "content" => "TST - Task2",
                "project-id" => "273775",
                "completed" => "false",
                "progress" => "60",
                "responsible-party-id" => "159667",
                "responsible-party-names" => "Someone N."
            ),
            "2" => array(
                "id" => "12046739",
                "content" => "TST - Task3",
                "project-id" => "723764",
                "completed" => "true",
                "progress" => "100",
                "responsible-party-id" => "123983",
                "responsible-party-names" => "Someone Else"
            )
        )
);
这基本上是我的问题。为了更好地解释我自己,我将把我想要的输出和最后的数组,以便您可以测试代码

今天$todayArray的预期产出:

foreach ($developers as $key => $value) {
for ($h=0; $h < count($data["todo-items"]); $h++) {
    if ($value == $data["todo-items"][$h]['responsible-party-id']) {
        $todayArray[$value]["id"] = $value;
        $todayArray[$value]["responsibleName"] [] = $data["todo-items"][$h]['responsible-party-names'];
        $todayArray[$value]["responsibleName"] ["project"][] = $data["todo-items"][$h]['project-id'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["id"] = $data["todo-items"][$h]['id'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["content"] []= $data["todo-items"][$h]['content'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["progress"] []= $data["todo-items"][$h]['progress'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["completed"][] = $data["todo-items"][$h]['completed'];
    }
  }
}
Array
(
[159667] => Array //responsible ID as key 
    (
        [id] => 159667 //responsible ID
        [responsibleName] => Array
            (
                [0] => Someone N. // This is stored as an array but it will only contain this same name in all positions as we are inside his id
                [project] => Array
                    (
                        [0] => 273774 // ID of the project (maybe tasks from different projects here)
                        [task] => Array //This is the structure that I want to repeat the most (one per task) 
                            (
                                [id] => 12046737 // ID of the task
                                [content] => Array
                                    (
                                        [0] => TST - Task1
                                        [1] => TST - Task2 //This don't belong in here, it should be under another task id
                                )
                                    )

                                [progress] => Array
                                    (
                                        [0] => 30
                                        [1] => 30 //This don't belong in here it should be under another task id
                                    )

                                [completed] => Array
                                    (
                                        [0] => false
                                        [1] => false //This don't belong in here it should be under another task id
                                    )

                            )

                        [1] => 273775 // The second tasks it's from a different project so it should be printed here or if was from the same project above this. 
                    )

                [1] => Someone N. // The task is assigned to the same person (because of the if condition) so this shouldn't be needed
        )
            )

    )

   //The next structure maybe ok, but it only works if the responsible has only one task assigned, and still has too many arrays inside it I guess

[123983] => Array
    (
        [id] => 123983
        [responsibleName] => Array
            (
                [0] => Someone Else
                [project] => Array
                    (
                        [0] => 723764
                        [task] => Array
                            (
                                [id] => 12046739
                                [content] => Array
                                    (
                                        [0] => TST - Task3
                                    )

                                [progress] => Array
                                    (
                                        [0] => 100
                                    )

                                [completed] => Array
                                    (
                                        [0] => true
                                    )

                            )

                    )

            )

    )

)
    Array
      (
      [159667] => Array
        (
            [id] => 159667
            [responsibleName] => Someone N.
                    [project] => Array
                        (
                            [0] => 273774
                            [task] => Array
                                (
                                    [id] => 12046737
                                    [content] =>  TST - Task1
                                    [progress] => 30
                                    [completed] => false
                                )
                             [1] => 273775
                             [task] => Array
                                 (
                                     [id] => 12046738
                                     [content] =>  TST - Task2
                                     [progress] => 30
                                     [completed] => false
                                 )

                        )
        )        
    [123983] => Array
      (
          [id] => 123983
          [responsibleName] => Someone Else
                  [project] => Array
                      (
                           [0] => 723764
                          [task] => Array
                             (
                                [id] => 12046739
                                [content] =>  TST - Task3
                                [progress] => 100
                                [completed] => true
                              )
                      )
      )
)
$developers= array(
 "0"=>  "163289",
 "1"=>  "159667",
 "2"=>  "91889",
 "3"=>  "123983"
);

$data =  array(
 "todo-items" => array(
            "0" => array(
                "id" => "12046737",
                "content" => "TST - Task1",
                "project-id" => "273774",
                "completed" => "false",
                "progress" => "30",
                "responsible-party-id" => "159667",
                "responsible-party-names" => "Someone N."
            ),
            "1" => array(
                "id" => "12046738",
                "content" => "TST - Task2",
                "project-id" => "273775",
                "completed" => "false",
                "progress" => "60",
                "responsible-party-id" => "159667",
                "responsible-party-names" => "Someone N."
            ),
            "2" => array(
                "id" => "12046739",
                "content" => "TST - Task3",
                "project-id" => "723764",
                "completed" => "true",
                "progress" => "100",
                "responsible-party-id" => "123983",
                "responsible-party-names" => "Someone Else"
            )
        )
);
需要复制的阵列:

foreach ($developers as $key => $value) {
for ($h=0; $h < count($data["todo-items"]); $h++) {
    if ($value == $data["todo-items"][$h]['responsible-party-id']) {
        $todayArray[$value]["id"] = $value;
        $todayArray[$value]["responsibleName"] [] = $data["todo-items"][$h]['responsible-party-names'];
        $todayArray[$value]["responsibleName"] ["project"][] = $data["todo-items"][$h]['project-id'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["id"] = $data["todo-items"][$h]['id'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["content"] []= $data["todo-items"][$h]['content'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["progress"] []= $data["todo-items"][$h]['progress'];
        $todayArray[$value]["responsibleName"] ["project"] ["task"] ["completed"][] = $data["todo-items"][$h]['completed'];
    }
  }
}
Array
(
[159667] => Array //responsible ID as key 
    (
        [id] => 159667 //responsible ID
        [responsibleName] => Array
            (
                [0] => Someone N. // This is stored as an array but it will only contain this same name in all positions as we are inside his id
                [project] => Array
                    (
                        [0] => 273774 // ID of the project (maybe tasks from different projects here)
                        [task] => Array //This is the structure that I want to repeat the most (one per task) 
                            (
                                [id] => 12046737 // ID of the task
                                [content] => Array
                                    (
                                        [0] => TST - Task1
                                        [1] => TST - Task2 //This don't belong in here, it should be under another task id
                                )
                                    )

                                [progress] => Array
                                    (
                                        [0] => 30
                                        [1] => 30 //This don't belong in here it should be under another task id
                                    )

                                [completed] => Array
                                    (
                                        [0] => false
                                        [1] => false //This don't belong in here it should be under another task id
                                    )

                            )

                        [1] => 273775 // The second tasks it's from a different project so it should be printed here or if was from the same project above this. 
                    )

                [1] => Someone N. // The task is assigned to the same person (because of the if condition) so this shouldn't be needed
        )
            )

    )

   //The next structure maybe ok, but it only works if the responsible has only one task assigned, and still has too many arrays inside it I guess

[123983] => Array
    (
        [id] => 123983
        [responsibleName] => Array
            (
                [0] => Someone Else
                [project] => Array
                    (
                        [0] => 723764
                        [task] => Array
                            (
                                [id] => 12046739
                                [content] => Array
                                    (
                                        [0] => TST - Task3
                                    )

                                [progress] => Array
                                    (
                                        [0] => 100
                                    )

                                [completed] => Array
                                    (
                                        [0] => true
                                    )

                            )

                    )

            )

    )

)
    Array
      (
      [159667] => Array
        (
            [id] => 159667
            [responsibleName] => Someone N.
                    [project] => Array
                        (
                            [0] => 273774
                            [task] => Array
                                (
                                    [id] => 12046737
                                    [content] =>  TST - Task1
                                    [progress] => 30
                                    [completed] => false
                                )
                             [1] => 273775
                             [task] => Array
                                 (
                                     [id] => 12046738
                                     [content] =>  TST - Task2
                                     [progress] => 30
                                     [completed] => false
                                 )

                        )
        )        
    [123983] => Array
      (
          [id] => 123983
          [responsibleName] => Someone Else
                  [project] => Array
                      (
                           [0] => 723764
                          [task] => Array
                             (
                                [id] => 12046739
                                [content] =>  TST - Task3
                                [progress] => 100
                                [completed] => true
                              )
                      )
      )
)
$developers= array(
 "0"=>  "163289",
 "1"=>  "159667",
 "2"=>  "91889",
 "3"=>  "123983"
);

$data =  array(
 "todo-items" => array(
            "0" => array(
                "id" => "12046737",
                "content" => "TST - Task1",
                "project-id" => "273774",
                "completed" => "false",
                "progress" => "30",
                "responsible-party-id" => "159667",
                "responsible-party-names" => "Someone N."
            ),
            "1" => array(
                "id" => "12046738",
                "content" => "TST - Task2",
                "project-id" => "273775",
                "completed" => "false",
                "progress" => "60",
                "responsible-party-id" => "159667",
                "responsible-party-names" => "Someone N."
            ),
            "2" => array(
                "id" => "12046739",
                "content" => "TST - Task3",
                "project-id" => "723764",
                "completed" => "true",
                "progress" => "100",
                "responsible-party-id" => "123983",
                "responsible-party-names" => "Someone Else"
            )
        )
);

我希望你能给我指出正确的方向。

好吧,我本来打算等到你澄清你的问题,但当用户发布不清楚问题的答案时,我会非常恼火,所以我会假设项目id值是唯一的,可以用作最深子数组的键

重要的是,似乎根本没有理由运行嵌套循环或访问
$developers
数组,所以我省略了这一部分。我希望这是尽可能干净/直接的

代码:()

输出:

array (
  159667 => 
  array (
    'id' => '159667',
    'responsibleName' => 'Someone N.',
    'projects' => 
    array (
      273774 => 
      array (
        'id' => '12046737',
        'content' => 'TST - Task1',
        'progress' => '30',
        'completed' => 'false',
      ),
      273775 => 
      array (
        'id' => '12046738',
        'content' => 'TST - Task2',
        'progress' => '60',
        'completed' => 'false',
      ),
    ),
  ),
  123983 => 
  array (
    'id' => '123983',
    'responsibleName' => 'Someone Else',
    'projects' => 
    array (
      723764 => 
      array (
        'id' => '12046739',
        'content' => 'TST - Task3',
        'progress' => '100',
        'completed' => 'true',
      ),
    ),
  ),
)

找到了,谢谢。理发。我会尽快看一看。所以你很乐意省略没有任务的开发人员。看起来是一个很好的最小样本。你真的需要'id'号的冗余存储吗?等一下,你有一个不可能的输出数组结构。请澄清有关任务的索引/键控应如何进行。看起来您在同一级别上有两个
任务
键。是的!你说得对,实际上数组结构更适合我。谢谢!