Group keys in an array

2

How to group identical keys and values in an array?

There may be N arrays inside the array, you can not add the same keys.

For example:

 [attributes] => Array
    (
        [0] => Array
            (
                [title] => Cor
                [type] => text
                [values] => Array
                    (
                        [1] => Amarelo
                    )

            )

        [1] => Array
            (
                [title] => Cor
                [type] => text
                [values] => Array
                    (
                        [2] => Azul
                    )

            )
        [2] => Array
            (
                [title] => Largura
                [type] => text
                [values] => Array
                    (
                        [2] => Grande
                    )

            )

Desired result:

 [attributes] => Array
    (
        [0] => Array
            (
                [title] => Cor
                [type] => text
                [values] => Array
                    (
                        [1] => Amarelo
                        [2] => Azul
                    )

            )
        [2] => Array
            (
                [title] => Largura
                [type] => text
                [values] => Array
                    (
                        [2] => Grande
                    )

            )
    
asked by anonymous 16.09.2014 / 17:13

2 answers

1

Not a great solution, because if the array is too large, it takes a long time. Let's say it is almost a gambiarra, because to use this solution and your problem should be deep.

$atrib = Array(
        0 => Array
            (
                "title" => "Cor",
                "type" => "text",
                "values" => Array
                    (
                        1 => "Amarelo"
                    )),
        1 => Array
            (
                "title" => "Cor",
                "type" => "text",
                "values" => Array
                    (
                        2 => "Azul"
                    )),
        2 => Array
            (
                "title" => "Largura",
                "type" => "text",
                "values" => Array
                    (
                        2 => "Grande"
                    )));


foreach ($atrib as &$A_value) {
    foreach ($atrib as $key=> $B_value) {
        if($A_value['title']==$B_value['title'])
        {
            if($A_value['values']!=$B_value['values'])
            {
                $A_value['values'] = array_merge($A_value['values'],$B_value['values']);
                unset($atrib[$key]);
            }

        }


    }


}
    
16.09.2014 / 19:47
0
<?php
$array3[] = '';
$array3[] = array("bola", "quadrado", "triangulo");
$array3[] = array("esfera", "quadrado", "triangulo");

$result1 = array_diff($array3[1], $array3[2]);
$result2 = array_diff($array3[2], $array3[1]);

$full = array_merge($result1, $result2);
echo '<pre>';
print_r($array3);
echo '<Hr>';
print_r($full);
echo '<Hr>';
//print_r($array2);
unset($array3[0]);
unset($array3[1]);
array_unshift($array3, $full);

print_r($array3);

?>
    
16.09.2014 / 18:28