在PHP上重新排列关联数组

时间:2018-05-25 05:09:33

标签: php arrays

在我写完这个问题后,我意识到代码太多了,但主要是因为我希望你理解我需要什么,并且你能够重现并修复这段代码。我觉得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'];
    }
  }
}

$ todayArray的输出是:

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
                                    )

                            )

                    )

            )

    )

)

这基本上是我的问题。为了更好地解释自己,我将把我想要的输出放在最后,最后是数组,这样你就可以测试代码了。

$ todayArray的预期输出:

    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"
            )
        )
);

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

1 个答案:

答案 0 :(得分:0)

好吧,我要等到你澄清了你的问题,但是当用户发出不清楚问题的答案时,我会感到非常恼火,所以我会假设project-id值是唯一的并且可以用作最深的子阵列的关键。

重要的是,似乎没有理由运行嵌套循环或完全访问$developers数组,因此我省略了该部分。我希望它可以像它一样干净/直接。

代码:(Demo

$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 ($data['todo-items'] as $item) {
    $result[$item['responsible-party-id']]['id'] = $item['responsible-party-id'];
    $result[$item['responsible-party-id']]['responsibleName'] = $item['responsible-party-names'];
    $result[$item['responsible-party-id']]['projects'][$item['project-id']] = [
        'id' => $item['id'],
        'content' => $item['content'],
        'progress' => $item['progress'],
        'completed' => $item['completed']
    ];
}
var_export($result);

输出:

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',
      ),
    ),
  ),
)