Regrouping repeated items from an array

Asked

Viewed 43 times

0

Good afternoon.

Currently I have a dynamic form where the person can add the following values:

Category | Title | Description

What I do so far: I take each line ((int) Category/ (string) Title / (String) Description) and insert it into my database by taking the ID it returns. The "Category" column contains fixed values in a droplist, and there may be lines with the same category, I need to GROUP these repeated items (or not) in another array

An example:

Array
(
    [0] => Array
        (
            [categoria] => 1 
            [titulo] => teste 1
            [descricao] => teste 1
            [id_no_banco] => 1
        )

    [1] => Array
        (
            [categoria] => 2
            [titulo] => teste 2
            [descricao] => teste 2
            [id_no_banco] => 2
        )

    [2] => Array
        (
            [categoria] => 1
            [titulo] => teste 3
            [descricao] => teste 3
            [id_no_banco] => 3
        )

)

What I need is: whether to repeat or not, create a second array that looks like this:

Array
(
    [0] => Array
        (
            [id_categoria] => 1
            [ids_do_banco] => 1;3
        )

    [1] => Array
        (
            [id_categoria] => 2
            [ids_do_banco] => 2
        )
)

Notice that the repeated categories of index 0 and 2 are "grouped" in the new array, and this is what I’m not finding a solution for, can someone give me a light? thank you

1 answer

0


Just make a simple foreach, considering:

$array = [
    [
        'categoria' => 1,
        'titulo' => 'teste 1',
        'descricao' => 'teste 1',
        'id_no_banco' => 1,
    ],
    [
        'categoria' => 2,
        'titulo' => 'teste 2',
        'descricao' => 'teste 2',
        'id_no_banco' => 2,
    ],
    [
        'categoria' => 1,
        'titulo' => 'teste 3',
        'descricao' => 'teste 3',
        'id_no_banco' => 3,
    ]
];

Do:

foreach($array as $item){

    if(isset($categoria[$item['categoria']])){
        $categoria[$item['categoria']] .= ';' . $item['id_no_banco'];
    }else{
        $categoria[$item['categoria']] = (string)$item['id_no_banco'];
    }

}

That would return:

array(2) {
  [1]=>
  string(3) "1;3"
  [2]=>
  string(1) "2"
}

Which may already be enough, where the array’s inputs/keys ([1] and [2]) represent the categoria and its value id_no_banco.


If you really want something like you mentioned I could use it:

foreach($array as $item){

    if(isset($categoria[$item['categoria']])){
        $categoria[$item['categoria']]['id_no_banco'] .= ';' . $item['id_no_banco'];
    }else{
        $categoria[$item['categoria']]['categoria'] =  $item['categoria'];
        $categoria[$item['categoria']]['id_no_banco'] = (string)$item['id_no_banco'];
    }

}

In this case it would still be possible to obtain the information by the index and there would also be the information of categoria and id_no_banco, resulting in:

array(2) {
  [1]=>
  array(2) {
    ["categoria"]=>
    int(1)
    ["id_no_banco"]=>
    string(3) "1;3"
  }
  [2]=>
  array(2) {
    ["categoria"]=>
    int(2)
    ["id_no_banco"]=>
    string(1) "2"
  }
}

You can use the array_values($categoria) or even sort($categoria) to reorder the array, thus breaking the 0 and not related to the category itself.


Another option would be to use array_search and the array_column, but doing so will make, internally, one more loop.

foreach($array as $item){

    $coluna = array_column($categoria, 'categoria');
    $index = array_search($item['categoria'], $coluna, true);

    if($index !== false){
        $categoria[$index]['id_no_banco'] .= ';' . $item['id_no_banco'];
    }else{
        $categoria[] = [
                        'categoria' =>  $item['categoria'],
                        'id_no_banco' => (string)$item['id_no_banco'],
                       ];
    }

}

Upshot:

array(2) {
  [0]=>
  array(2) {
    ["categoria"]=>
    int(1)
    ["id_no_banco"]=>
    string(3) "1;3"
  }
  [1]=>
  array(2) {
    ["categoria"]=>
    int(2)
    ["id_no_banco"]=>
    string(1) "2"
  }
}
  • That’s exactly what I needed, I broke my head so much for something relatively simple, thank you very much!

Browser other questions tagged

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