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);
1
  • Works like a charm, u r a star
    – user888300
    Commented Jul 12, 2013 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.