Separate and merge arrays

Asked

Viewed 176 times

-3

I have the following array:

    Array
(
    [0] => Array
        (
            [setor_loja] => 43
            [impressao] =>  (P) Pizza 2 + Pizza 2\n
        )

)
Array
(
    [1] => Array
        (
            [setor_loja] => 83
            [impressao] => Acai 300 ml\n
        )

    [2] => Array
        (
            [setor_loja] => 83
            [impressao] => AA§ai 500 ml\n
        )

)
Array
(
    [3] => Array
        (
            [setor_loja] => 33
            [impressao] => Misto c/ ovo\n
        )

    [4] => Array
        (
            [setor_loja] => 33
            [impressao] => Misto duplo\n
        )

)

I’d like the exit to be like this: (P) Pizza 2 + Pizza 2 n ( leave alone )

Finish 300 ml n + Açai 500 ml n ( leave together because these two products are from the same sector)

Mixed c/egg n + Double mixed n ( come out together as these two products are from the same sector)

  • 1

    I suggest formatting the output array in the same way as the input one, so it is clear.

  • What about your code? Looks like you forgot to post it.

  • vc just concatenate the output or generate a new array with all the elements of the sector?

  • How are you generating this array? The organization of its indexes are in sequence but still are arrays distinct with different products.

  • Hi, just concatenate the exit, then I’ll turn around. Hi Marcelo, this array is coming from a series of foreachs and selects to assemble it, a piece of code is this: $data[] = array( 'setor_loja'=>$setor_sector. $store, 'print'=>' . $product. ' n' );

1 answer

3


I set up a combination that creates a new array with all the products/items grouped by the store code.

array_column() extract all store ids, array_flip() invert the values with the array keys returned by array_column() your exit is something like:

Array
(
    [43] => 0
    [83] => 2
    [33] => 4
)

Then it is made a check if 'products' array exists the store id it copies the product description and plays in a new element.

$arr = array(
        array('setor_loja' => '43', 'impressao' => '(P) Pizza 2 + Pizza 2\n'),
        array('setor_loja' => '83', 'impressao' => 'Acai 300 ml\n'),
        array('setor_loja' => '83', 'impressao' => 'Açai 500 ml\n'),
        array('setor_loja' => '33', 'impressao' => 'Misto c/ ovo\n'),
        array('setor_loja' => '33', 'impressao' => 'Misto duplo\n')
);


$setores = array_flip(array_column($arr, 'setor_loja'));
$novo = array();
foreach($arr as $item){
    if(isset($setores[$item['setor_loja']])){
        $novo[$item['setor_loja']][] = $item['impressao'];
    }
}

The exit of $novo is:

Array
(
    [43] => Array
        (
            [0] => (P) Pizza 2 + Pizza 2\n
        )

    [83] => Array
        (
            [0] => Acai 300 ml\n
            [1] => Açai 500 ml\n
        )

    [33] => Array
        (
            [0] => Misto c/ ovo\n
            [1] => Misto duplo\n
        )

)

To display a store’s products you can use the function implode():

echo 'Loja 33 '. implode(' | ', $novo[33]);

Exit:

Loja 33 Misto c/ ovo\n | Misto duplo\n
  • Many thanks to all who responded, and to you my friend! I managed to implement my system with this solution.

Browser other questions tagged

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