Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

236
Vistas
¿Cómo dividir los objetos de una matriz por año de JSON usando PHP?

Mi pregunta es cómo obtener o dividir una matriz por año usando la fecha clave de la matriz en orden ascendente,

He probado muchos... pero no lo he conseguido,

 [ { "id": "47", "date": "07/16/2022", "text": "ph" } { "id": "46", "date": "06/16/2022", "text": "ph" }, { "id": "45", "date": "06/16/2021", "text": "ph" }]

Y la salida que necesito es,

 [ "2021": [{ "id": "45", "date": "06/16/2021", "text": "ph" }], "2022": [{ "id": "46", "date": "06/16/2022", "text": "ph" }, { "id": "47", "date": "07/16/2022", "text": "ip" }] ]

¿Cómo hacerlo en PHP o JavaScript?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Esta es una demostración sobre cómo transformar su matriz de entrada en el objeto de salida esperado usando javascript:

 const input = [ { "id": "47", "date": "07/16/2022", "text": "ph" }, { "id": "46", "date": "06/16/2022", "text": "ph" }, { "id": "45", "date": "06/16/2021", "text": "ph" } ]; const output = {}; //for each object in the input array for(o of input){ //parse the year part of the date property const year = o.date.substring(6); //if the parsed year doesn't exist yet in the output object if (!output.hasOwnProperty(year)) //then add an empty array to the year key in the output object output[year] = []; //add the current input object to the array bound to the year key in the output object output[year].push(o); } console.log( output );

Y esta es la misma lógica implementada usando php:

https://onlinephp.io/c/87510

 <?php $input = [ [ "id" => "47", "date" => "07/16/2022", "text" => "ph" ], [ "id" => "46", "date" => "06/16/2022", "text" => "ph" ], [ "id" => "45", "date" => "06/16/2021", "text" => "ph" ] ]; $output = []; //for each object in the input array foreach($input as $o){ //parse the year part of the date property $year = substr($o['date'], 6); //if the parsed year doesn't exist yet in the output object if (!array_key_exists($year, $output)) //then add an empty array to the year key in the output object $output[$year] = []; //add the current input object to the array bound to the year key in the output object $output[$year][] = $o; } var_dump( $output );
about 4 years ago · Juan Pablo Isaza Denunciar

0

La versión de PHP podría verse así:

 $input = [ [ "id" => "47", "date" => "07/16/2022", "text" => "ph" ], [ "id" => "46", "date" => "06/16/2022", "text" => "ph" ], [ "id" => "45", "date" => "06/16/2021", "text" => "ph" ] ]; $result = []; foreach ($input as $entry) { $date = new DateTime($entry['date']); $result[$date->format('Y')][] = $entry; } ksort($result);

Como se me preguntó en la respuesta de Diego, también ksort en la mezcla, que ordena la matriz resultante por las teclas en orden descendente.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda