Creating one array from another array in php

前端 未结 2 1397
故里飘歌
故里飘歌 2020-11-22 04:33

I have an array that looks like this. This is a 2 dimensional array.

$MainArray = Array
(
    [0] => Array
        (
            [Job_Name] => WXYZ
            


        
相关标签:
2条回答
  • 2020-11-22 05:14

    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
                   )
             )   
    )
    
    0 讨论(0)
  • 2020-11-22 05:18
    foreach ($MainArray as $value) {
        $name = $value['Machine_Name'];
        unset($value['Machine_Name']);
        $ConvertedArray[$name][] = $value;
    }
    
    0 讨论(0)
提交回复
热议问题