Creating one array from another array in php

2018-12-31 07:38发布

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.

标签: php arrays
2条回答
春风洒进眼中
2楼-- · 2018-12-31 07:52
foreach ($MainArray as $value) {
    $name = $value['Machine_Name'];
    unset($value['Machine_Name']);
    $ConvertedArray[$name][] = $value;
}
查看更多
流年柔荑漫光年
3楼-- · 2018-12-31 07:53

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
               )
         )   
)
查看更多
登录 后发表回答