将数组映射到新数组

时间:2019-09-17 19:31:07

标签: php arrays mapping

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

原始数组:

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

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

{{1}}

对于所有8(5和3)次幂值继续执行,以使结果数组为8个数组的数组。

2 个答案:

答案 0 :(得分: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']已被保留。可以根据需要轻松添加。

答案 1 :(得分:0)

您可以这样做:

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