Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
perl到php的翻译_Php_Perl_Arrays - Fatal编程技术网

perl到php的翻译

perl到php的翻译,php,perl,arrays,Php,Perl,Arrays,如何将此perl子例程转换为PHP函数 sub disagreement { my $disagreement = 0; my %aggregate = () ; foreach my $item (@_) {$aggregate{$item}++} foreach my $cat_a (keys %aggregate) { foreach my $cat_b (keys %aggregate) { if ($cat_a != $cat_b) {$disagr

如何将此perl子例程转换为PHP函数

sub disagreement {
  my $disagreement = 0;
  my %aggregate = () ;
  foreach my $item (@_) {$aggregate{$item}++}
  foreach my $cat_a (keys %aggregate) {
    foreach my $cat_b (keys %aggregate) {
      if ($cat_a != $cat_b) {$disagreement += $aggregate{$cat_a} * $aggregate{$cat_b} * &$distance_metric ($cat_a , $cat_b)}
    }
  }
  $disagreement /= @_ * (@_ - 1) ;
  return ($disagreement) ;
}
我这里有个问题:

foreach my $cat_a (keys %aggregate) {
    foreach my $cat_b (keys %aggregate) {
    }
}
变成

foreach ($aggregate as $key => $cat_a)

假设$aggregate是一个关联数组。

AFAIK,PHP中的所有数组都是关联的。在这个表达式中“&”的用途是什么:“…*&$distance\u metric($cat\u a,$cat\u b)}”?
// &$... is a value reference instead of a value copy, when in doubt use $...
foreach ($aggregate as $cat_a => &$val_a) {
  foreach ($aggregate as $cat_b => &$val_b) {
    if ($cat_a !== $cat_b) $disagreement += $val_a * $val_b ...;
  }
}
// &$... is a value reference instead of a value copy, when in doubt use $...
foreach ($aggregate as $cat_a => &$val_a) {
  foreach ($aggregate as $cat_b => &$val_b) {
    if ($cat_a !== $cat_b) $disagreement += $val_a * $val_b ...;
  }
}