Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Inserte elementos en la matriz donde falta un mes

Quiero llenar los meses que faltan en una matriz con ceros.

Datos de entrada:

 $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'];

Mi código:

 $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]; } }

He estado tratando de resolver este problema pero sin éxito.

Resultado Esperado:

 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 answers
Answer question

0

Iré con array_column() , array_diff() y un solo foreach()

Proceso:

1) Crear una matriz de todos los meses

2) Obtenga todo el mes presente en su matriz usando array_column() .

3) Encuentre los meses no disponibles en su matriz usando array_diff() .

4) Iterar sobre esta diferencia y agregarlos a su matriz actual.

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

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

over 4 years ago · Santiago Trujillo Report

0

Algo como esto podría funcionar:

 <?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 Report

0

Dado que el objetivo aquí es agregar los meses faltantes a la matriz original $d sin cambiar los elements existentes (según el resultado esperado), simplemente podemos cambiar $d en lugar de crear una nueva variable $res .

También convertir las claves de la matriz $d hará que sea mucho más fácil trabajar con ella. Esto supone que el month aparece solo una vez en $d .

Enlace al ejemplo de trabajo

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

Si el month no es único en la matriz, puede usar array_search o in_array para obtener resultados similares.

 $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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!