将数组映射到新数组

Posted

技术标签:

【中文标题】将数组映射到新数组【英文标题】:Mapping array to new array 【发布时间】:2020-01-18 17:03:08 【问题描述】:

我有一个数组,其中有一个数组,我想为每个“power”数组值创建一个新数组。最好的方法是什么?

原始数组:

Array
(
    [0] => Array
        (
            [date] => 2019-09-16 15:16:03
            [stid] => 64
            [beam] => 1
            [power] => 1,3,7,8,9
            [gates] => 5
)

   [1] => Array
        (
            [date] => 2019-09-16 15:17:03
            [stid] => 64
            [beam] => 1
            [power] => 14,15,16
            [gates] => 3
)
)

编辑:输出数组应该从 1 开始递增门

Array
(
    [0] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 1
            [power] => 1
        )

   [1] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 2
            [power] => 3
         )

   [2] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 3
            [power] => 7
         )

   [3] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 4
            [power] => 8
         )


   [4] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 5
            [power] => 9
         )

   [5] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 1
            [power] => 14
         )

   [6] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 2
            [power] => 15
         )

   [7] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 3
            [power] => 16
         )
)

对所有 8(5 和 3)个幂值继续,这样得到的数组是 8 个数组的数组。

【问题讨论】:

【参考方案1】:
# Original Data Structure
$arr = 
[
    [
        'date' => '2019-09-16 15:16:03',
        'stid' => 64,
        'beam' => 1,
        'power' => '1,3,7,8,9',
        'gates' => 5
    ],
    [
        'date' => '2019-09-16 15:17:03',
        'stid' => 64,
        'beam' => 1,
        'power' => '14,15,16',
        'gates' => 3
    ]
];

$output = [];
foreach ($arr as $a)

    $gateCount = 0;
    foreach (explode(',', trim($a['power'], '')) as $p) 
    
        $output[] = [
            'date' => $a['date'],
            'gate' => $gateCount++ % $a['gates'] + 1,
            'power' => $p
        ];
    

print_r($output);

输出:

Array
(
    [0] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 1
            [power] => 1
        )

    [1] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 2
            [power] => 3
        )

    [2] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 3
            [power] => 7
        )

    [3] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 4
            [power] => 8
        )

    [4] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 5
            [power] => 9
        )

    [5] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 1
            [power] => 14
        )

    [6] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 2
            [power] => 15
        )

    [7] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 3
            [power] => 16
        )

)

注意:

['beam']['stid'] 根据您的示例已被忽略。如果需要,可以轻松添加。

【讨论】:

这是真的。在我对数据结构的示例重建中,我只是将其设为了一个数组, 我修改了我的答案,让结构使用x,y 格式和foreach 相应地。 另外,在这个例子中,门应该等于 0,1,2,3,4,5,0,1,2。我该如何添加? @mcook16,编辑您的原始问题以指定应如何填充门索引。 @mcook16 您希望如何定义您的'gate'【参考方案2】:

你可以这样做:

$newArray = [];
foreach ($oldArray as $element) 
   foreach ($element['power'] as $power) 
      $element['power'] = $power;
      $newArray[] = $element;
   

【讨论】:

以上是关于将数组映射到新数组的主要内容,如果未能解决你的问题,请参考以下文章

将对象数组过滤到新数组中

如何将具有相同属性的所有字典从数组中获取到新数组?

React-native 将渲染数组中的值插入到新数组中

将数组作为url参数传递并重定向到新页面

Javascript:将数组选择到新数组中(如 C# Select)

javascript数组。将连续的值添加到新数组中