-3

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.

MaverickPablo
  • 21
  • 1
  • 6
  • 1
    What you tried to get specific output as you want..! – Kausha Mehta Feb 18 '16 at 05:01
  • @Mav how do we know which subarrays should retain the `Machine_Name` element in the output? See how `Machine1` subarrays don't include the `Machine_Name` elements, yet the `Machine2` subarray does? This makes your question an Unclear "requirements dump". – mickmackusa Dec 14 '20 at 06:34
  • Duplicate of: https://stackoverflow.com/q/12706359/2943403 – mickmackusa Dec 14 '20 at 13:51
  • 1
    Does this answer your question? [How to group subarrays by a column value?](https://stackoverflow.com/questions/12706359/how-to-group-subarrays-by-a-column-value) – Vega Dec 14 '20 at 15:23

2 Answers2

2

Use below code:-

$result = [];
foreach($MainArray as $record){
 $result[$record['Machine_Name']][] = $record;
}
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • This unexplained snippet is telling fibs about its output. Wanna know how I can tell that you simply copied the output from the OP's question body? `Machine_Name` is only in the last subarrays' data. – mickmackusa Dec 14 '20 at 06:36
  • @mickmackusa : Thanks for pointing it out. Answered it 4 years ago so not exactly the idea but yeah `Machine_Name` key was missing. Just removed the output. I think OP just wanted to distinguish the data based on `Machine_Name`. – Ravi Hirani Dec 14 '20 at 09:04
  • Now you only need to accompany your snippet with an explanation. I use old pages to close new duplicates, but I cannot use this page in good confidence to educate users because 1. the question isn't 100% clear and 2. the answers are not explained. – mickmackusa Dec 14 '20 at 09:14
0
foreach ($MainArray as $value) {
    $name = $value['Machine_Name'];
    unset($value['Machine_Name']);
    $ConvertedArray[$name][] = $value;
}
Nick
  • 9,735
  • 7
  • 59
  • 89