I have an array that looks like this. This is a 2 dimensional array.
$MainArray = Array
(
[0] => Array
(
[Job_Name] => WXYZ
Use below code:-
$result = [];
foreach($MainArray as $record){
$result[$record['Machine_Name']][] = $record;
}
echo '<pre>'; print_r($result);
output:-
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
)
)
)
foreach ($MainArray as $value) {
$name = $value['Machine_Name'];
unset($value['Machine_Name']);
$ConvertedArray[$name][] = $value;
}