Group keys from an array

Asked

Viewed 1,523 times

2

How to group keys and identical values of an array?

There may be N arrays inside the array, you cannot add equal 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
                    )

            )
  • 1

    What is the difficulty? I don’t understand.

  • Do dynamically, being that the array can change, I need to group only when the title and type are equal, color and width are examples, but can come any value, when equal, group the values.

  • You do an insert function on array, compare the title with existing ones and if there is an equal adds to the values

  • That’s where I’m catching rsrrsrsrsrs, I’m stuck on it. rs

  • It is you who create the right array?

  • No, it comes from the comic book, can have 0 or 1000 results.

  • It is complicated to do this without consuming great resources... When I get home I see.

  • Then it seemed simple when I looked. rs

Show 3 more comments

2 answers

1


It’s not a great solution, because if the array is too large, it takes a long time. Let’s say it’s almost a gambiarra, because to use this solution and your problem must 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]);
            }

        }


    }


}

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);

?>
  • ops... will always be 3 indexes that array?

  • if $array[0], $array[1] / $array[1], $array[0]

  • Dude, it’s a single array, with several internal arrays, which have another array, it doesn’t work the same way, the default is exactly what I put in the question.

  • Uai compares array[1] with array[0]

  • Maybe I got you wrong, but it doesn’t work here.

  • test with the example I gave you, string would be quiet, but try with array inside array.

  • redoz it with array inside array

  • @Marceloaymone, then test there edited dinovo... see if it is this

  • or you will have to do what Marty bb Falow

  • Yes, but my array brings up to 100 arrays within it, and in other situations only 4 or 5, how would you? has to be dynamic.

  • then it is better to create a function that verifies what exists in the array... and then add the contents ... if you will not have to give too much back to organize this array

  • http://php.net/manual/en/ref.array.php a look ai has function that checks what is inside the array... ai fica facin

Show 7 more comments

Browser other questions tagged

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