Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
Push elements into array where a month is missing

I want to fill missing months in an array with zeros.

Input Data:

$d = [
        [
          'type' =>  25500,
          'month' => 'July'
         ],
         [
          'type' => 5465,
          'month' => 'January'
         ],
         [
          'type' => 40000,
          'month' => 'April'
         ],
         [
          'type' => 35000,
          'month' => 'June'
         ],
         [
          'type' => 10000,
          'month' => 'February'
     ]
 ];

$allmonths = ['January','February','March', 'April','May','June','July','August','September','October','November','December'];

My Code:

$res = [];
foreach ($allmonths  as $key => $mes) {
    $teste = array_search($mes, array_column($d, 'month'));
    if ($teste) {
        $res[$teste] = ['type' => 2, 'moth '=> $mes];
    } else {
        $res[] = ['type' => 0,'moth '=> $mes];
    }
}

I have been trying to solve this problem but with no success.

Expected Result:

Array
(
    [0] => Array
        (
            [type] => 25500
            [month] => July
        )
    [1] => Array
        (
            [type] => 5465
            [month] => January
        )
    [2] => Array
        (
            [type] => 40000
            [month] => April
        )
    [3] => Array
        (
            [type] => 35000
            [month] => June
        )
    [4] => Array
        (
            [type] => 10000
            [month] => February
        )
    [5] => Array
        (
            [type] => 0
            [month] => March
        )
    [6] => Array
        (
            [type] => 0
            [month] => May
        )
    [7] => Array
        (
            [type] => 0
            [month] => August
        )
    [8] => Array
        (
            [type] => 0
            [month] => September
        )
    [9] => Array
        (
            [type] => 0
            [month] => October
        )
    [10] => Array
        (
            [type] => 0
            [month] => November
        )
    [11] => Array
        (
            [type] => 0
            [month] => December
        )
)
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I will go with array_column(), array_diff() and a single foreach()

Process:

1)Create an array of all months

2)Get all month present in your array using array_column().

3)Find the not available months in your array using array_diff().

4)Iterate over this difference and add them to your current array.

$allMonths = ['January','February','March', 'April','May','June','July','August','September','October','November','December'];

$currentMonths = array_column($d, 'month');

$notIncludedMonth = array_diff($allMonths,$currentMonths);

foreach ($notIncludedMonth as $month) {
    $d[] = [
        'type' => 0,
        'month' => $month,
    ];
}

print_r($d);

Output : https://3v4l.org/GsFoO

over 4 years ago · Santiago Trujillo Relatório

0

Something like this could work:

<?php

$d = [
       [
         'type' =>  25500,
         'month' => 'July'
        ],
        [
         'type' => 5465,
         'month' => 'January'
        ],
        [
         'type' => 40000,
         'month' => 'April'
        ],
        [
         'type' => 35000,
         'month' => 'June'
        ],
        [
         'type' => 10000,
         'month' => 'February'
        ]
      ];

$allmonths = ['January','February','March', 'April','May','June','July','August','September','October','November','December'];

$present_months = array_column($d, 'month');

foreach ($allmonths as $month) {
    if (!in_array($month, $present_months)) {
        $d[] = [
            'type' => 0,
            'month' => $month,
        ];
    }
}

var_dump($d);
over 4 years ago · Santiago Trujillo Relatório

0

Since the objective here is to append missing months to the original array $d without changing the existing elements (as per the expected result), we can simply change $d instead of creating a new variable $res.

Also converting the keys of the array $d will make it much easier to work with. This does assume that month appears only once in $d.

Link to working example

## Assumption: Month is unique
$dNew = array_column($d, null, 'month');
$res = [];
foreach ($allmonths as $key => $mes) {
    if (!isset($dNew[$mes])) {
        $d[] = ['type' => 0, 'moth ' => $mes];
    }
}
var_dump($d);

If month is not unique in the array you can use array_search or in_array to get similar results.

$dNew = array_column($d,  'month');
$res = [];
foreach ($allmonths as $key => $mes) {
    if (!array_search($mes, $dNew)) {
        $d[] = ['type' => 0, 'moth ' => $mes];
    }
}
print_r($d);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda