Remove duplicate keys from Multidimensional Array

泄露秘密 提交于 2019-12-01 01:44:31

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)

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

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?

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!