Grouping of the Array?

Asked

Viewed 84 times

1

I got the following array:

Array
(
    [0] => Array
        (
            [0] => 
        )

    [1] => Array
        (
            [0] => Cor
            [1] => Azul
        )

    [2] => Array
        (
            [0] => Peso
            [1] => 100kg
        )

    [3] => Array
        (
            [0] => Espessura
            [1] => 10cm
        )

    [4] => Array
        (
            [0] => 
        )

    [5] => Array
        (
            [0] => Cor
            [1] => Azul
        )

    [6] => Array
        (
            [0] => 
        )

    [7] => Array
        (
            [0] => Cor
            [1] => adg
        )

    [8] => Array
        (
            [0] => Peso
            [1] => gadg
        )

    [9] => Array
        (
            [0] => 
        )

    [10] => Array
        (
            [0] => Cor
            [1] => Preto
        )

    [11] => Array
        (
            [0] => Espessura
            [1] => 325
        )

)

and need to be grouped as follows:

Array
(
    [Cor] => Array
        (
            [0] => Azul 
            [1] => adg
            [2] => Preto
        )

    [Peso] => Array
        (
            [0] => 100kg
            [1] => gadg
        )

    [Espessura] => Array
        (
            [0] => 10cm
            [1] => 325
        )
)

What would be the best way to achieve this result in php?

  • How is this generated array? Depending on how it is, it will be better to modify its creation than just adapt it. By the way, what you have tried to do?

  • What is the rule?

  • Good morning Anderson, it would be while ($Row = mysqli_fetch_assoc($result)){ $descri = $Row['product description']; //ex. $descri = " - Color: Blue - Thickness: 10cm"; $test = explode(' - ', $descri); foreach ($test as $key => $value) { if ($test != '') { $teste2 = explode(': ', $value,2); $arr_test[] = $teste2; }; }; };

2 answers

3


If you always come to this structure, it solves:

$arr = [ 
    [''], 
    ['Cor', 'Azul'], 
    ['Peso', '100kg'],
    ['Espessura', '10cm'],
    [''],
    ['Cor', 'Azul'],
    [''],
    ['Cor', 'adg'],
    ['Peso', 'gadg'],
    [''],
    ['Cor', 'Preto'],
    ['Espessura', '325']
];

$n = [];
foreach($arr as $value) {
    if(isset($value[1])) {
        if(!in_array($value[1], $n[$value[0]])) {
            $n[$value[0]][] = $value[1];
        }
    }
}

echo '<pre>'; print_r($n);

Exit:

Array
(
    [Cor] => Array
        (
            [0] => Azul
            [1] => adg
            [2] => Preto
        )

    [Peso] => Array
        (
            [0] => 100kg
            [1] => gadg
        )

    [Espessura] => Array
        (
            [0] => 10cm
            [1] => 325
        )

)
  • 2

    Thank you so much @Juniornunes, this is exactly what I needed.

0

I made a reduced example:

<?php

    $ar = array(
            array(),
            array('cor', 'azul'),
            array('peso', '192kg'),
            array('espessura', '10cm'),
            array(),
            array('cor', 'azul'),
            array(),
            array('cor', 'adg'),
            array('peso', 'adg'),
            array(),            
            array('cor', 'preto'),          
            array('espessura', '325')
        );

    $ar1 = array();
    foreach($ar as $value)
    {
        if (is_array($value) && count($value) == 2)
        {
            $ar1[$value[0]][] = $value[1];
        }
    }

    var_dump($ar1);

Online Example

Browser other questions tagged

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