1

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.

5 Answers 5

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

It is potentially unlimited in depth, but I don't think i will ever have an instance over 4.
1

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

2 Comments

Thanks, but I am trying to maintain the original array structure because I need maintain the hierarchy. Your code merges the array down.
What do you mean it merges the array down? It doesn't. There is no way it can since the only operation done on the original array is unset. See the output above.
0

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?

1 Comment

I could, but this array is cached and I don't want to create unnecessary database queries...
0

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

1 Comment

The issue is, I am trying to print out the hierarchy, so if I move it into an array that has the parents, I am going to have to rebuild the format of the original array again.
0

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.

Comments

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.