多维数组在任务分配中不起作用

时间:2018-01-22 13:25:59

标签: php arrays multidimensional-array

这里我有一个阵列使用这个我必须制作我期望的数组格式,我必须在数组内我尝试但我不能使我的预期数组格式,如果有人知道的意思请更新我的答案

  

的print_r($ productsArray)

    Array
(
    [0] => Array
        (
            [allocation_date] => 2018-01-01
            [t_project] => 10001
            [t_assign_to] => G2E0357
            [t_start_time] => 01:30 PM
            [t_end_time] => 11:30 AM
            [allocated_day] => 2018-01-01
        )

    [1] => Array
        (
            [allocation_date] => 2018-01-02
            [t_project] => 10001
            [t_assign_to] => G2E0357
            [t_start_time] => 01:30 PM
            [t_end_time] => 11:30 AM
            [allocated_day] => 2018-01-02
        )

    [2] => Array
        (
            [allocation_date] => 2018-01-03
            [t_project] => 10001
            [t_assign_to] => G2E0357
            [t_start_time] => 01:30 PM
            [t_end_time] => 11:30 AM
            [allocated_day] => 2018-01-03
        )

    [3] => Array
        (
            [allocation_date] => 2018-01-18
            [t_project] => 10008
            [t_assign_to] => XPL0315
            [t_start_time] => 11:30 AM
            [t_end_time] => 07:30 PM
            [allocated_day] => 2018-01-18
        )

    [4] => Array
        (
            [allocation_date] => 2018-01-19
            [t_project] => 10008
            [t_assign_to] => XPL0315
            [t_start_time] => 11:30 AM
            [t_end_time] => 07:30 PM
            [allocated_day] => 2018-01-19
        )

    [5] => Array
        (
            [allocation_date] => 2018-01-20
            [t_project] => 10008
            [t_assign_to] => XPL0315
            [t_start_time] => 11:30 AM
            [t_end_time] => 07:30 PM
            [allocated_day] => 2018-01-20
        )

    [6] => Array
        (
            [allocation_date] => 2018-01-25
            [t_project] => 10008
            [t_assign_to] => G2E0357
            [t_start_time] => 10:30 AM
            [t_end_time] => 01:30 PM
            [allocated_day] => 2018-01-25
        )

    [7] => Array
        (
            [allocation_date] => 2018-01-26
            [t_project] => 10008
            [t_assign_to] => G2E0357
            [t_start_time] => 10:30 AM
            [t_end_time] => 01:30 PM
            [allocated_day] => 2018-01-26
        )

    [8] => Array
        (
            [allocation_date] => 2018-01-27
            [t_project] => 10008
            [t_assign_to] => G2E0357
            [t_start_time] => 10:30 AM
            [t_end_time] => 01:30 PM
            [allocated_day] => 2018-01-27
        )

)
  

使用上面我必须像这个数组(我期望的结果)

Array
(
    [G2E0357] => Array
        (
            [10001] => Array
              (
                [2018-01-01] => t_start_time (value should come here)
                [2018-01-02] => t_start_time (value should come here)
                [2018-01-03] => t_start_time (value should come here)
              )

              [10008] => Array
              (
                [2018-01-25] => t_start_time (value should come here)
                [2018-01-26] => t_start_time (value should come here)
                [2018-01-27] => t_start_time (value should come here)
              )
        )

    [XPL0315] => Array
        (
            [10001] => Array
              (
                [2018-01-18] => t_start_time (value should come here)
                [2018-01-19] => t_start_time (value should come here)
                [2018-01-20] => t_start_time (value should come here)
              )
        )

)
  1. G2E0357& XPL0315(t_assign_to values)
  2. 10001& 10008(t_project值)
  3. 2018-01-25(assigned_day values)
  4.   

    我试过但我无法达到预期的结果

        $project_name = array();
    foreach ($productsArray as $key => $value) {
        $project_name[$value['t_assign_to']][$value['allocated_day']]= $value['t_project'];
    }
    
      

    的print_r($ PROJECT_NAME);

     Array
    (
        [G2E0357] => Array
            (
                [2018-01-01] => 10001
                [2018-01-02] => 10001
                [2018-01-03] => 10001
                [2018-01-25] => 10008
                [2018-01-26] => 10008
                [2018-01-27] => 10008
            )
    
        [XPL0315] => Array
            (
                [2018-01-18] => 10008
                [2018-01-19] => 10008
                [2018-01-20] => 10008
            )
    
    )
    

2 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

foreach($productsArray as $key0 => $info) {
    $key1 = $info['t_assign_to'];
    $key2 = $info['t_project'];
    $key3 = $info['allocated_day'];
    $project_name[$key1][$key2][$key3] = $info['t_start_time']; 
}

这是我用代码得到的(不完整):

array (
  'G2E0357' => 
  array (
    10001 => 
    array (
      '2018-01-01' => '01:30 PM',
      '2018-01-02' => '01:30 PM',
      '2018-01-03' => '01:30 PM',
    ),
    10008 => 
    array (
      '2018-01-25' => '10:30 AM',
    ),
  ),
  'XPL0315' => 
  array (
    10008 => 
    array (
      '2018-01-18' => '11:30 AM',
      '2018-01-19' => '11:30 AM',
      '2018-01-20' => '11:30 AM',
    ),
  ),
)

答案 1 :(得分:0)

以相反的方式使用相同的代码,试试这个

$project_name = array();
foreach ($productsArray as $key => $value) {
$project_name[$value['t_assign_to']]$value['t_project']]= array([$value['allocated_day']=> "t_start_time (value should come here)");
}