PHP将null添加到数组值(如果不存在)

时间:2018-07-16 08:14:44

标签: php

我正在生成数据数组,并想将0 / null添加到一个特定的月份,该月份不存在任何数据,但是已经为之苦苦挣扎了几天。

我正在生成这样的最终数据集数组

foreach ($patients as $key => $item) {
    $labels[] = $item['month'];
    usort($labels, "compare_months");
    $labels = array_unique($labels);

    $chartData[ $item['brand_name'] ]['data'][]     = 
    $item['patientsCount'];
    $chartData[ $item['brand_name'] ]['brand_data'] = [
        "name"  => $item['brand_name'],
        "color" => $item['color']
    ];
}

foreach ($chartData as $item) {
    $pointRadius++;

    $dataSet[] = [
        'label'           => $item['brand_data']['name'],
        'data'            => $item['data'],
        'pointRadius'     => $pointRadius,
        'fill'            => false,
        'borderWidth'     => 1,
        'backgroundColor' => "#" . $item['brand_data']['color'],
        'borderColor'     => "#" . $item['brand_data']['color'], 
    ];
}

$finalData[] = [
    'labels'   => $labels,
    'datasets' => $dataSet
];

哪个给我这个json响应

[
  {
    "labels":[
      "April",
      "May",
      "June",
      "July"
    ],
    "datasets":[
      {
        "label":"Medicine 1",
        "data":[
          1
        ],
        "pointRadius":1,
        "fill":false,
        "borderWidth":1,
        "backgroundColor":"#ea5f2d",
        "borderColor":"#ea5f2d"
      },
      {
        "label":"Medicine 2",
        "data":[
          1,
          1,
          1
        ],
        "pointRadius":2,
        "fill":false,
        "borderWidth":1,
        "backgroundColor":"#ffb400",
        "borderColor":"#ffb400"
      },
      {
        "label":"Medicine 3",
        "data":[
          1,
          1,
          2
        ],
        "pointRadius":3,
        "fill":false,
        "borderWidth":1,
        "backgroundColor":"#ff7777",
        "borderColor":"#ff7777"
      },
      {
        "label":"Medicine 4",
        "data":[
          1,
          1,
          2
        ],
        "pointRadius":4,
        "fill":false,
        "borderWidth":1,
        "backgroundColor":"#64a36f",
        "borderColor":"#64a36f"
      },
      {
        "label":"Medicine 5",
        "data":[
          2
        ],
        "pointRadius":5,
        "fill":false,
        "borderWidth":1,
        "backgroundColor":"#e7e6fc",
        "borderColor":"#e7e6fc"
      }
    ]
  }
]

如您所见,对于某些项目,数据对象仅包含1个错误的值,因为我需要在没有数据的月份中添加零。

这可以在MySQL中完成吗?还是使用PHP更好?如何使用?

编辑1

这是我当前得到的数组。

Array
(
    [MEDICINE 1] => Array
        (
            [April] => 1
        )

    [MEDICINE 2] => Array
        (
            [April] => 1
            [July] => 2
        )

    [MEDICINE 3] => Array
        (
            [April] => 1
            [May] => 1
            [July] => 2
        )

    [MEDICINE 4] => Array
        (
            [May] => 1
            [July] => 3
        )

    [MEDICINE 5] => Array
        (
            [June] => 2
        )

    [MEDICINE 6] => Array
        (
            [July] => 1
        )

)

您可以看到有些药品只有一个月的数据,如果没有数据,我想将这些月份添加到值为0的药品数组中。

1 个答案:

答案 0 :(得分:0)

在每个override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait } override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { return .portrait } override var shouldAutorotate: Bool{ return false } foreach之后添加此行。因为如果数据长度小于2,则需要跳过。

if (count($item['data']) < 2) continue;