OM The Eternity
OM The Eternity

Reputation: 16224

Need an Array Count On Common Values

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

Upvotes: 0

Views: 350

Answers (2)

Matthew
Matthew

Reputation: 48304

$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

Upvotes: 2

Scotty Waggoner
Scotty Waggoner

Reputation: 3380

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.

Upvotes: 0

Related Questions