0

I am Using PHP and Mysql, Apachhe

I Have an Array

Array
(
    [2] => Array
        (
            [0] => 6
            [1] => 2
        )

    [1] => Array
        (
            [0] => 6
            [1] => 2
        )

)

Here The middle container is the Document ID and the final children are userids, Here I am looking for a way to find a method to count the common user ids among all the different Documents. I also need it specified that a particular user id available in which documentids...

could Some one guide me to achieve this?

Thanks in Advance, OM

1
  • Your question is a little ambiguous. I've supplied an answer, but if it's not what you're looking for then try to write some clear examples of what you expect the output to be for the above data. Commented Mar 28, 2011 at 7:22

2 Answers 2

2
$unique_users = array();

foreach ($docs as $doc_id => $users)
{
  foreach ($users as $user_id)
  {
    if (!isset($unique_users[$user_id]))
      $unique_users[$user_id] = array($doc_id);
    else
      $unique_users[$user_id][] = $doc_id;
  }
}

echo count($unique_users);  // number of users

var_dump($unique_users[1]); // list of documents user_id #1 is in
Sign up to request clarification or add additional context in comments.

Comments

0

Add all the ids from the first element to an array. Then continue to loop through the other elements and keep only the ids that are also in the current element.

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.