Robert Syvarth
Robert Syvarth

Reputation: 11

Remove duplicate keys from Multidimensional Array

I have a rather large array that contains data for all of the forums on a message board, unfortunately I am running into an issue where I am having repeat entries for some keys. The array is ordered in an hierarchy by parents, which is why it gets deep at some points.

Array
(
    [0] => Array
        (
            [cat_data] => Array()
            [forum_data] => Array
                (
                    [2] => Array
                        (
                            [subforums] => Array
                                (
                                    [6] => Array
                                        (
                                            [subforums] => Array
                                                (
                                                    [15] => Array()
                                                    [16] => Array()
                                                )
                                        )
                                    [7] => Array()
                                    [15] => Array()
                                    [16] => Array()
                                )
                        )
                    [3] => Array()
                )
        )
)

The subforums on the forum id 6 are repeated as subforums for forum id 2. I need to remove the repeated keys that are in the lowest level of the array. So in this example, I would like to keep 15 and 16 as subs of 6 but remove them as subs of 2.

Just a note, I am writing an application for the board, I am not generating the array, it is generated by the board, that is why I can't remove the duplicates while the array is being created.

Thank you all for your help.

Upvotes: 1

Views: 2580

Answers (5)

netcoder
netcoder

Reputation: 67695

This should do it:

function remove_dup_keys(array &$array) {
    $keys = array();
    foreach ($array as $key => &$value) {
        if (is_array($value)) {
            $keys = array_merge($keys, remove_dup_keys($value));
        }
    }

    foreach ($keys as $key) {
        if (is_numeric($key) && in_array($key, $keys)) {
            unset($array[$key]);
        }
    }

    $keys = array_merge($keys, array_keys($array));
    return $keys;
}

remove_dup_keys($yourarray);

You get:

Array
(
    [0] => Array
        (
            [cat_data] => Array
                (
                )
            [forum_data] => Array
                (
                    [2] => Array
                        (
                            [subforums] => Array
                                (
                                    [6] => Array
                                        (
                                            [subforums] => Array
                                                (
                                                    [15] => Array
                                                        (
                                                        )
                                                    [16] => Array
                                                        (
                                                        )
                                                )
                                        )
                                    [7] => Array
                                        (
                                        )
                                )
                        )
                    [3] => Array
                        (
                        )
                )
        )
)

Upvotes: 1

Zoltan Lengyel
Zoltan Lengyel

Reputation: 360

You should loop through the array with a recursive function, something like this:

function remove_dupes(&$arr,$keys = array()) {
  if (is_array($arr['subforums']) {
    $keys = remove_redundants($arr['subforums'],$keys);
  }
  foreach ($arr as $k => $v) {
    if (in_array($k,$keys)) {
      unset($arr[$k]);
    } else {
      $keys[] = $k;
    }
  }
  return $keys;
}

remove_dupes($forumarray[forum_data]);

This will go to the deepest parts first (because the first call is itself) and work it's way backwards.

Upvotes: 0

evan
evan

Reputation: 12535

As you said, you're just getting back this array and need to do something with it.

My recommendation is to walk the array and creating a new one that is easier to deal with.

The new array would look like:

array(
    'forum_id' => array(
        'forum_data' => //whatever,
        'parent_forum => // id of parent - greatest id seen as parent
    ),
    ...
);

Upvotes: 0

Henze
Henze

Reputation: 76

Maybe you should change the way you create those arrays. it is better to prevent this from happening

edit: I see...and you don't have the board hosted your self? how deep can a forum go? (like a subforum in a subforum etc in a forum)

Upvotes: 1

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

You should loop through all arrays and apply http://php.net/manual/en/function.array-unique.php edit: that's not going to work in this case :)

Why can't you generate new array that suits you a ditch this?

Upvotes: 0

Related Questions