Reroute repeated items from an array

0

Good afternoon.

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

Category | Contact Us | Description

What I do so far: I get each line ((int) Category / (string) Title / (String) Description) and I enter in my database taking the ID that it returns. The column "Category" contains fixed values in a droplist, and it may happen to have rows with the same category, I need to GROUP these repeating (or not) items 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 indexes 0 and 2 are "grouped" in the new array, and this is what I can not find a solution, can someone give me a light? Thank you

    
asked by anonymous 21.06.2017 / 20:00

1 answer

0

Just do 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,
    ]
];

Make:

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

}

This would return:

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

That may already be enough, where indexes / array keys ( [1] and [2] ) represent categoria and its value id_no_banco .

If you really want something like mentioned you could use:

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 get the information through 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 array_values($categoria) or even sort($categoria) to reorder the array, thus breaking 0 and not related to the category itself.

Another option would be to use array_search and array_column , but doing so will internally do 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'],
                       ];
    }

}

Result:

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"
  }
}
    
21.06.2017 / 20:36