0

I'm getting stack-removing duplicated keys and assigning them to a new array.

My array:

  array     (
            [1] => Array
                (
                    [name] => name1
                    [actions] => add
                )

            [2] => Array
                (
                    [name] => name1
                    [actions] => remove
                )

            [3] => Array
                (
                    [name] => name2
                    [actions] => dosomething1
                )
            [4] => Array
                (
                    [name] => name2
                    [actions] => dosomething1
                )

        )

What I am trying to achieve:

 array    (
                [1] => Array
                    (
                        [name] => name1
                        [actions] => add
                        [actions] => remove
                    )         
                [2] => Array
                    (
                        [name] => name2
                        [actions] => dosomething1
                        [actions] => dosomething1
                    ) 

            )

What i have tried:

    public function array_unique_multidimensional($input)
{
    $serialized = array_map('serialize', $input);
    $unique = array_unique($serialized);
    return array_intersect_key($input, $unique);
}

It is incorrectly returning the same array. Any help would be appreciated.

0

1 Answer 1

1

You cannot have two array keys with the save values (so two actions elements for a given element would not be possible) What you can do is have a single action element with multiple values in it.

  $results = array();
  foreach ($array as $v){
      if (!isset($results[$v["name"]]){
           $results[$v["name"]] = array("name"=>$v["name"], "actions"=>array($v["actions"]));
      } else {
          $results[$v["name"]]["actions"][] = $v["actions"];
      }
  }

if you want to remove the string keys on the top level array you can then do.

 $results = array_values($results);
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm, u r a star

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.