在php中从另一个数组创建一个数组 [英] Creating one array from another array in php

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

问题描述

我有一个看起来像这样的数组.这是一个二维数组.

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
        )
 )

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

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
               )
         )   
)

请对此提供任何帮助,我们将不胜感激.我遇到了一些问题,需要弄清楚如何使用这个原始数组创建新数组.所以基本上我将每台机器上的所有作业组合在一起,这些作业的键取决于它们在原始数组中的位置.因此,如果原始数组有一个键为 2 的作业并且该机器上没有其他作业具有更高的键,那么它将成为该作业的键 0 并使用该机器名称创建一个新键.

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天全站免登陆