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

225
Visualizações
How to split an array's objects by year of JSON using PHP?

My question is how to get or split an array by year using in array key date by ascending order,

I've tried many... but I didn't get it,

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

And the output I need is,

[
"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"
}]
]

How to do it in either PHP or JavaScript?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This is a demo on how to transform your input array in the expected output object using 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 );

And this is the same logic implemented using 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 Relatório

0

The PHP version could look like this:

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

As asked on Diego's answer I've thrown ksort into the mix as well, which sorts the resulting array by the keys in descending order.

about 4 years ago · Juan Pablo Isaza 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