一尘不染

在PHP中从另一个数组创建一个数组

php

我有一个看起来像这样的数组。这是一个二维数组。

$MainArray = Array
(
    [0] => Array
        (
            [Job_Name] => WXYZ
            [Quantity] => 1000
            [Machine_Name] => Machine1
            [Start_Date] => 2014-07-30 00:00:00
            [Completion_Date] => 2014-08-02 00:00:00
            [Labor] => 4
        )
    [1] => Array
        (
            [Job_Name] => ABCD
            [Quantity] => 1500
            [Machine_Name] => Machine2
            [Start_Date] => 2014-08-08 00:00:00
            [Completion_Date] => 2014-08-14 00:00:00
            [Labor] => 2
        )
    [2] => Array
        (
            [Job_Name] => BCDA
            [Quantity] => 1200
            [Machine_Name] => Machine1
            [Start_Date] => 2014-08-02 00:00:00
            [Completion_Date] => 2014-08-07 00:00:00
            [Labor] => 1
        )
 )

我想使用此信息来创建一个新的三维数组,如下所示。

$ConvertedArray = Array
(
    [Machine1] => Array
        (
            [0] => Array
               (
                  [Job_Name] => WXYZ
                  [Quantity] => 1000
                  [Start_Date] => 2014-07-30 00:00:00
                  [Completion_Date] => 2014-08-02 00:00:00
                  [Labor] => 4
               )
            [1] => Array
               (
                  [Job_Name] => BCDA
                  [Quantity] => 1200
                  [Start_Date] => 2014-08-02 00:00:00
                  [Completion_Date] => 2014-08-07 00:00:00
                  [Labor] => 1
               )
         )
      [Machine2] => Array
        (
            [0] => Array
               (
                  [Job_Name] => ABCD
                  [Quantity] => 1500
                  [Machine_Name] => Machine2
                  [Start_Date] => 2014-08-08 00:00:00
                  [Completion_Date] => 2014-08-14 00:00:00
                  [Labor] => 2
               )
         )   
)

请对此有任何帮助。我陷入困境,需要弄清楚如何使用此原始数组创建新数组。因此,基本上,我将每台计算机上的所有作业分组在一起,而这些作业的密钥取决于它们在原始阵列中的状态。因此,如果原始阵列上有一个键为2的作业,而该机上没有其他作业具有更高的键,则它将变成该作业的键0,并使用该机器名创建一个新键。

非常感谢您的帮助。


阅读 288

收藏
2020-05-26

共1个答案

一尘不染

foreach ($MainArray as $value) {
    $name = $value['Machine_Name'];
    unset($value['Machine_Name']);
    $ConvertedArray[$name][] = $value;
}
2020-05-26