0

I have a function in php:

function cmp_key($lst){
    $itersect_size = count(array_intersect($zset, $lst)); //zset is a list which i have
    return $intersect_size,-count($lst)
}

and then this code in python:

list_with_biggest_intersection = max(iterable,key = cmp_key)

how can i do the above line of code in php given that i want to use the php function cmp_key as the key for the max function...

tenstar
  • 9,816
  • 9
  • 24
  • 45
  • So what is $zset? It's not passed into the PHP function so I don't see how this code works, also what does the cmp_key function return? – Dale Jun 16 '13 at 06:31

2 Answers2

0

Call the function to pass the return value as parameter of max function.

list_with_biggest_intersection = max(iterable, cmp_key($lst));
Nagarjun
  • 2,346
  • 19
  • 28
  • Can you please see my previous post: http://stackoverflow.com/questions/16963958/most-of-in-python I want to do something similar in php @Nagarjun – tenstar Jun 16 '13 at 07:16
  • I am not good at python, probably @jordojuice's answer is what you are looking for. – Nagarjun Jun 16 '13 at 08:25
0

Duplicating @mgilson's answer in Python, here's the equivalent in PHP.

function cmp_key($set, $list) {
  return count(array_intersect($set, $list));
}

// This iterates over all lists and compares them with some
// original list, here named $set for consistency with the other example.
$largest = NULL;
foreach ($lists as $list) {
  if (!isset($largest)) {
    $largest = array('list' => $list, 'count' => cmp_key($set, $list));
  }
  else {
    $count = cmp_key($set, $list);
    if ($count > $largest['count']) {
      $largest = array('list' => $list, 'count' => $count);
    }
  }
}
$list_with_biggest_intersection = $largest['list'];
kuujo
  • 7,785
  • 1
  • 26
  • 21