从PHP另一个阵列创建一个阵列 [英] Creating one array from another array in php

查看:141
本文介绍了从PHP另一个阵列创建一个阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,看起来像这样。这是一个2维阵列

  $ MainArray =阵列

    [0] =>排列
        (
            [JOB_NAME] => WXYZ
            [数量] => 1000
            [MACHINE_NAME] =>计算机1
            [START_DATE] => 2014年7月30日00:00:00
            [COMPLETION_DATE] => 2014年8月2日00:00:00
            [劳动] => 4
        )
    [1] =>排列
        (
            [JOB_NAME] => A B C D
            [数量] => 1500
            [MACHINE_NAME] =>机2
            [START_DATE] => 2014-06-19 00:00:00
            [COMPLETION_DATE] => 2014年8月14日00:00:00
            [劳动] => 2
        )
    [2] =>排列
        (
            [JOB_NAME] => BCDA
            [数量] => 1200
            [MACHINE_NAME] =>计算机1
            [START_DATE] => 2014年8月2日00:00:00
            [COMPLETION_DATE] => 2014年8月7日00:00:00
            [劳动] => 1
        )
 )

我想用这个信息来创建一个新的3维数组,看起来像这样。

  $ ConvertedArray =阵列

    [计算机1] =>排列
        (
            [0] =>排列
               (
                  [JOB_NAME] => WXYZ
                  [数量] => 1000
                  [START_DATE] => 2014年7月30日00:00:00
                  [COMPLETION_DATE] => 2014年8月2日00:00:00
                  [劳动] => 4
               )
            [1] =>排列
               (
                  [JOB_NAME] => BCDA
                  [数量] => 1200
                  [START_DATE] => 2014年8月2日00:00:00
                  [COMPLETION_DATE] => 2014年8月7日00:00:00
                  [劳动] => 1
               )
         )
      [计算机2] =>排列
        (
            [0] =>排列
               (
                  [JOB_NAME] => A B C D
                  [数量] => 1500
                  [MACHINE_NAME] =>机2
                  [START_DATE] => 2014-06-19 00:00:00
                  [COMPLETION_DATE] => 2014年8月14日00:00:00
                  [劳动] => 2
               )
         )

请有这方面的帮助将是AP preciated。我坚持的东西,需要弄清楚如何创建使用这个原数组新的数组。所以基本上我从分组每台机器的所有工作在一起,这些工作的关键取决于他们是如何原来的数组中为止。因此,如果原始数组具有与键2工作,并没有其他作业有这台机器上一个更高的关键,那么它将成为该作业0键并使用该机器名新的密钥。

我真的AP preciate你对这个帮助。


解决方案

 的foreach($ MainArray为$值){
    $名称= $值['MACHINE_NAME'];
    未设置($值['MACHINE_NAME']);
    $ ConvertedArray [$名称] [] = $价值;
}

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.

解决方案

foreach ($MainArray as $value) {
    $name = $value['Machine_Name'];
    unset($value['Machine_Name']);
    $ConvertedArray[$name][] = $value;
}

这篇关于从PHP另一个阵列创建一个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆