在php中从另一个数组创build一个数组

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

$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 ) ) 

我想用这个信息来创build一个看起来像这样的新的三维数组。

 $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 ) ) ) 

请任何帮助,这将不胜感激。 我坚持的东西,需要弄清楚如何使用这个原始数组创build新的数组。 所以基本上我把每台机器的所有工作分组在一起,这些工作的密钥取决于他们在原始数组中的方式。 因此,如果原始数组使用密钥2进行作业,并且没有其他作业在该机器上具有较高密钥,则该作业将成为密钥0,并使用该计算机名称创build一个新密钥。

我非常感谢你的帮助。

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

使用下面的代码: –

 $result = []; foreach($MainArray as $record){ $result[$record['Machine_Name']][] = $record; } echo '<pre>'; print_r($result); 

输出: –

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