Home > Software design >  How to add missing keys to array
How to add missing keys to array

Time:11-19

I have an array that needs to contain the same data keys for each time, is it possible to add the missing keys set to 0 given the following array?

enter image description here

I'm trying to take that and produce this:

[
  {
    "name": "7:00 AM",
    "data": [
      {
        "x": "2019",
        "y": 1
      },
      {
        "x": "2020", // Added
        "y": 0       // Added
      },
      {
        "x": "2021", // Added
        "y": 0       // Added
      }
    ]
  },
  {
    "name": "8:00 AM",
    "data": [
      {
        "x": "2019",
        "y": 69
      },
      {
        "x": "2020",
        "y": 4
      },
      {
        "x": "2021",
        "y": 221
      }
    ]
  },
  {
    "name": "9:00 AM",
    "data": [
      {
        "x": "2019",
        "y": 6
      },
      {
        "x": "2020",
        "y": 2
      },
      {
        "x": "2021", // Added
        "y": 0       // Added
      }
    ]
  }
]

So basically the longest data array was in the 8:00 AM time slot, I'm trying to add those missing data points to the other time slots wherever missing with a default value of 0

EDIT: Here is what I have to produce the first array:

I'm using Laravel and querying the DB with the following:

$shipments = Orders::whereNotNull('shipped_at')->groupBy(DB::raw('YEAR(created_at)'))->selectRaw('count(*) as count, HOUR(shipped_at) as hour')->selectRaw("DATE_FORMAT(created_at, '%Y') label")->orderBy('label')->orderBy('hour')->groupBy('hour')->get();

And then I'm formatting the array like this, just not sure how to add in those missing keys:

$shipping_chart_year = [];
foreach ($shipments as $item) {
    $hour = Carbon::parse($item['hour'] . ':00:00')->timezone('America/Chicago')->format('g:i A');
    $shipping_chart_year[] = ['name' => $hour, 'data' => [['x' => $item['label'], 'y' => $item['count']]]];
}

$shipping_chart_year = collect($shipping_chart_year)
    ->groupBy('name')
    ->transform(function ($items, $name) {
        // Merge nested arrays into one
        $data = $items->map(function ($item) {
            return $item['data'][0];
        })->toArray();

        return [
            'name' => $name,
            'data' => $data,
        ];
    })
    ->values() 
    ->toArray();

CodePudding user response:

function getAllYears(array $array): array
{
    $allKeys = [];
    foreach ($array as $data) {
        foreach ($data['data'] as $years) {
            if (!in_array($years['x'], $allKeys, true)) {
                $allKeys[] = $years['x'];
            }
        }
    }
    sort($allKeys);
    return $allKeys;
}

function addMissingKeys(array $array): array
{
    $allYears = getAllYears($array);
    foreach ($array as $key => $data) {
        $currentYears = array_map(static function ($year) {
            return $year['x'];
        }, $data['data']);
        $yearToAdd = array_diff($allYears, $currentYears);
        foreach ($yearToAdd as $year) {
            $array[$key]['data'][] = [
                'x' => $year,
                'y' => 0,
            ];
        }
    }
    return $array;
}

$array = [
    [
        "name" => "7:00 AM",
        "data" => [
            [
                "x" => "2019",
                "y" => 1
            ]
        ]
    ],
    [
        "name" => "8:00 AM",
        "data" => [
            [
                "x" => "2019",
                "y" => 69
            ],
            [
                "x" => "2020",
                "y" => 4
            ],
            [
                "x" => "2021",
                "y" => 221
            ]
        ]
    ],
    [
        "name" => "9:00 AM",
        "data" => [
            [
                "x" => "2019",
                "y" => 6
            ],
            [
                "x" => "2020",
                "y" => 2
            ]
        ]
    ]
];

$array = addMissingKeys($array);

var_dump($array);

You can see result of this code here: https://sandbox.onlinephpfunctions.com/code/2c58df4749126199bf21357d1c615634a0ebd629

  • Related