Group the values of a PHP array according to the same key.

Asked

Viewed 289 times

0

Good afternoon, you guys, I know the question may seem repetitive, but I’m already a while looking for the solution to my case, especially here in #stack.

I got the following Array():

array (size=9)
'Janeiro' => 
array (size=54)
  0 => 
    array
      'nome' => string 'Pessoa1' 
      'valor' => string '9' 
  1 => 
    array
      'nome' => string 'Pessoa2' 
      'valor' => string '2' 
  2 => 
    array
      'nome' => string 'Pessoa1' 
      'valor' => null 
  3 => 
    array
     'nome' => string 'Pessoa2' 
      'valor' => null
  4 => 
    array
      'nome' => string 'Pessoa3' 
      'valor' => null 
'Fevereiro' => 
 array
  0 => 
    array
      'nome' => string 'Pessoa1' (
      'valor' => string '28' 
  1 => 
    array
      'nome' => string 'Pessoa2' 
      'valor' => string '17' (length=2)
  2 => 
    array
      'nome' => string 'Pessoa3' 
      'valor' => string '2' (length=1)
  3 => 
    array
      'nome' => string 'Pessoa1' 
      'valor' => null
  4 => 
    array
      'nome' => string 'Pessoa2' 
      'valor' => null
  5 => 
    array
      'nome' => string 'Pessoa3' 
      'valor' => null
  6 => 
    array
      'nome' => string 'Pessoa4' 
      'valor' => null

... And so on containing every month these days. I’m trying to return an array that looked like this:

 array
'Pessoa1' => 
array
  meses => v1,v2,v3,v4,v5,v6,v7,v8,v9 
   array
'Pessoa2' => 
array
  meses => v1,v2,v3,v4,v5,v6,v7,v8,v9 

And so on catching all the "People", only as shown in exe. has month that the value of the person is null and has month that it has value, so I need to keep the values null, ex [null,null,2,3,4,5,6,null,null,]

  • https://answall.com/questions/53302/

  • @Noobsaibot I used this example as a base but could not return as I posted in the question.

  • But the same person appears more than once in certain months. How does it work in this case ? If you can build a complete example it is ideal to be easy to answer

  • 1

    I don’t think it is possible otherwise, but to run a logic of loops and go through the array to find all people.... and another loop traversing again to assign in what months and their values that person appears in the array.... I think it has to be in the same race.... rsrsrs A complete example would be interesting to test and propose the solution of a more interesting algorithm... T+

  • @I’ll try to explain the context. I have the month array within that array I have the person array that will contain its name and value and yes I have within the month array a list of all the people in my BD who in case would be with the value null + the people who have any data registered in the value field, then I have mes1[person=> name = person1 value = 10, name = person1 value = null] there in that case if it has the value field other than null replaces, someone had posted a Cód almost with the solution but it deleted the answer.

  • Well if case is still n clear I can create a bin of the array and post here, because it is a little large.

Show 1 more comment

1 answer

2


Following the structure of the presented array you can follow a simple iteration in the data and build the future array:

<?php

$registroGeral = [
    'Janeiro' => [
        [
            'nome' => 'Pessoa1',
            'valor' => '9'
        ],
        [
            'nome' => 'Pessoa2',
            'valor' => '2' 
        ],
        [
            'nome' => 'Pessoa1',
            'valor' => null 
        ]
    ],
    'Fevereiro' => [
        [
            'nome' => 'Pessoa1',
            'valor' => '28',
        ],
        [
            'nome' => 'Pessoa2',
            'valor' => '17',
        ],
        [
            'nome' => 'Pessoa3',
            'valor' => '2',
        ],
        [
            'nome' => 'Pessoa1',
            'valor' => null,
        ],
        [
            'nome' => 'Pessoa2',
            'valor' => null,
        ],
        [
            'nome' => 'Pessoa3',
            'valor' => null,
        ],
        [
            'nome' => 'Pessoa4',
            'valor' => null,
        ]
    ]
];

$frequenciaParticipante = [];
foreach ($registroGeral as $mes => $registros) {
    foreach ($registros as $registro) {
        if (!array_key_exists($registro['nome'], $frequenciaParticipante)) {
            $frequenciaParticipante[$registro['nome']] = [];
        }

        if (!in_array($mes, $frequenciaParticipante[$registro['nome']])) {
            $frequenciaParticipante[$registro['nome']][] = $mes;
        }
    }
}

var_dump($frequenciaParticipante); exit;

The final result will look like:

array(4) {
  'Pessoa1' =>
  array(2) {
    [0] =>
    string(7) "Janeiro"
    [1] =>
    string(9) "Fevereiro"
  }
  'Pessoa2' =>
  array(2) {
    [0] =>
    string(7) "Janeiro"
    [1] =>
    string(9) "Fevereiro"
  }
  'Pessoa3' =>
  array(1) {
    [0] =>
    string(9) "Fevereiro"
  }
  'Pessoa4' =>
  array(1) {
    [0] =>
    string(9) "Fevereiro"
  }
}

In the end just manipulate the participant’s months as you want

  • good, that would be just, I just need to adjust a little more the output if you can help me thank you, I made this edition at Cod, $frequenciaParticipante[$registro['nome']][] = $mes; $frequenciaParticipante[$registro['nome']][] = $registro['valor']; and got the following return array 'Pessoa1' => array 0 => 'Janeiro' 1 => '9' 2 => 'Fevereiro' 3 => '28' 4 => 'Marco' 5 => '18'.... If I withdraw the $mes and leave only the value it returns to me the value and null together and with $mes no, in case now I’m trying to remove the $mes and leave only the values so it stays like this months = [v1,v2,v3,v4]

  • Exit without the $mes 'Pessoa1' => &#xA; array &#xA; 0 => null&#xA; 1 => '2'&#xA; 2 => null&#xA; 3 => null&#xA; 4 => '3'&#xA; 5 => null&#xA; 6 => '2'&#xA; 7 => null&#xA; 8 => '5'&#xA; 9 => null&#xA; 10 => null&#xA; 11 => '2'&#xA; 12 => null&#xA; 13 => '11'&#xA; 14 => null

  • Exit with the $mes 'Pessoa1' => &#xA; array&#xA; 0 => 'Janeiro' &#xA; 1 => null&#xA; 2 => 'Fevereiro'&#xA; 3 => '2'&#xA; 4 => 'Marco'&#xA; 5 => null&#xA; 6 => 'Abril'&#xA; 7 => '3' &#xA; 8 => 'Maio'&#xA; 9 => '2'&#xA; 10 => 'Junho'&#xA; 11 => '5'&#xA; 12 => 'Julho'&#xA; 13 => null&#xA; 14 => 'Agosto'&#xA; 15 => '2'&#xA; 16 => 'Setembro'&#xA; 17 => '11'

  • 1

    I did, I did the following by storing $mes worth in $months and then adding this Cod $arr = [];&#xA;foreach ($frequenciaParticipante as $key => $value) {&#xA; $resultado = array_diff($value, $meses);&#xA; $arr[$key] = $resultado;&#xA;} Thank you so much for the help, I was already days behind a solution to this, vlw even.

  • 1

    I’m glad you got Diogo. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.