PHP 从数组中的数组形成自定义数组

PHP Forming Custom Array from Array within array

我有一个三个月的数组,其中包含 三个月的数据,它包含 储蓄和折扣信息,如下所示:

graphData 保存 DataArr.

[DataArr] => 数组

    [0] => Array
            (
             [data] => Array
                 (
                   [0] => Array 
                      ( 
                        [Month] => 10 Jan 2019
                        [Cost] => 60.3
                        [Name] => Savings
                      )
                   [1] => Array 
                      ( 
                        [Month] => 10 Feb 2019
                        [Cost] => 45.3
                        [Name] => Savings
                      )
                   [2] => Array 
                      ( 
                        [Month] => 10 Mar 2019
                        [Cost] => 50.6
                        [Name] => Savings
                      )
                )
           )
    [1] => Array
            (
             [data] => Array
                 (
                   [0] => Array 
                      ( 
                        [Month] => 10 Jan 2019
                        [Cost] => 89.62
                        [Name] => Discount
                      )
                   [1] => Array 
                      ( 
                        [Month] => 10 Feb 2019
                        [Cost] => 20.2
                        [Name] => Discount
                      )
                   [2] => Array 
                      ( 
                        [Month] => 10 Mar 2019
                        [Cost] => 0.0
                        [Name] => Discount
                      )
                )
           )

需要数组 - 传递到 Morris.js 折线图

现在我想遍历这个数组并希望数组格式如下

           [data] => Array
                 (
                   [0] => Array 
                      ( 
                        [Month] => 10 Jan 2019
                        [Savings] => 60.3
                        [Discount] => 89.62

                      )
                   [1] => Array 
                      ( 
                        [Month] => 10 Feb 2019
                        [Saving] => 45.3
                        [Discount] => 20.2
                      )
                   [2] => Array 
                      ( 
                        [Month] => 10 Mar 2019
                        [Saving] => 50.6
                        [Discount] => 0.0
                      )
                )

你能告诉我如何使用 PHP 将其放入所需的数组吗?我也尝试了 array_push 和其他方法以及 JQuery 数组方法,但我的所有努力都失败了,过去 3 天我一直在处理这个问题,非常感谢任何帮助。

甚至尝试使用 PHP foreach 但即使这样也失败了,遗漏了一些东西并且没有更多关于如何让它工作的想法!如果你们中的任何人对此有很多想法,请告诉我。

谢谢

编辑

**我在 PHP 中尝试过的示例代码 **

for($k = 0; $k < sizeof($graphData['DataArr']); $k++ ) {
                for($l = 0; $l < sizeof($graphData['DataArr'][$k]); $l++ ) {
                    array_push($graphData['DataArr'], array(
                        'label' => $graphData['DataArr'][$k][$l]['label'], 
                        'percent' => $graphData['DataArr'][$k][$l]['percentage']
                            )
                    );
                }
            }

此处标签 => COST、DISCOUNT 和 Percentage 是与标签关联的值。

<?php

$graphData =
    array(
        'DataArr' => 
        array
            (
            array(
             'data' => array
                 (
                   array 
                      ( 
                        'Month' => '10 Jan 2019',
                        'Cost' => 60.3,
                        'Name' => 'Savings'
                      ),
                   array 
                      ( 
                        'Month' => '10 Feb 2019',
                        'Cost' => 45.3,
                        'Name' => 'Savings'
                      ),
                   array 
                      ( 
                        'Month' => '10 Mar 2019',
                        'Cost' => 50.6,
                        'Name' => 'Savings'
                      )
                )
            ),
            array
            (
             'data' => array
                 (
                   array 
                      ( 
                        'Month' => '10 Jan 2019',
                        'Cost' => 89.62,
                        'Name' => 'Discount'
                      ),
                   array 
                      ( 
                        'Month' => '10 Feb 2019',
                        'Cost' => 20.2,
                        'Name' => 'Discount'
                      ),
                   array 
                      ( 
                        'Month' => '10 Mar 2019',
                        'Cost' => 0.0,
                        'Name' => 'Discount'
                    )
                )
           )
    )
);


$result = [];

foreach($graphData['DataArr'] as $value){
    foreach($value['data'] as $details){
        if(!isset($result['data'][$details['Month']])) $result['data'][$details['Month']]  = ['Month' => $details['Month']];
        $result['data'][$details['Month']][$details['Name']] = $details['Cost'];
    }
}

$result['data'] = array_values($result['data']); // to eliminate keys
print_r($result);

演示: https://3v4l.org/QMVuR

  • 我们首先创建一个 $result 数组,其中包含 data 键。
  • 我们遍历 $graphData 并将 MonthSavingsDiscount 的值存储在 Month.[=29 的索引键值下=]
  • 最后,我们array_values()删除密钥。

根据您在此处提供的内容,需要生成新数组的代码:

<?php
$data = [
  [
    'data' => [
      [
        'Month' => '10 Jan 2019',
        'Cost' => 60.3,
        'Name' => 'Savings'
      ],
      [
        'Month' => '10 Feb 2019',
        'Cost' => 45.3,
        'Name' => 'Savings'
      ],
      [
        'Month' => '10 Mar 2019',
        'Cost' => 50.6,
        'Name' => 'Savings'
      ]
    ],
  ],
  [
    'data' => [
      [
        'Month' => '10 Jan 2019',
        'Cost' => 89.62,
        'Name' => 'Discount'
      ],
      [
        'Month' => '10 Feb 2019',
        'Cost' => 20.2,
        'Name' => 'Discount'
      ],
      [
        'Month' => '10 Mar 2019',
        'Cost' => 0.0,
        'Name' => 'Discount'
      ]
    ],
  ]
];

$new_arr = ['data' => []];
echo "<pre>";

foreach( $data[0]['data'] as $key => $value) {
  // $new_arr['data'][$k]['Month'] = 
  $new_arr['data'][$key]['Month'] =  $value['Month'];
  $new_arr['data'][$key]['Discount'] =  $value['Cost'];
  $new_arr['data'][$key]['Savings'] =  $data[1]['data'][$key]['Cost'];
}

print_r($new_arr);

注意:此代码假定 $data 数组将只有 2 个主要元素,此数组中的更多元素可能会导致不同的行为。