I have an array that looks like this. This is a 2 dimensional array.
$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
)
)
I want to use this information to create a new 3 dimensional array that looks like this.
$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
)
)
)
Please any help on this would be appreciated. I am stuck with something and need to figure out how to create the new array using this original array. So basically I am grouping all the jobs from each machine together and the keys for those jobs depend on how they are in the original array. So if the original array has a job with the key 2 and no other job has a higher key on that machine, then it will become key 0 for that job and create a new key with that machine name.
I really appreciate your help on this.
Use below code:-
output:-