0

嗨,伙计们有以下数组:

 Array
   (
    [0] => Array
      (
        [SERIAL] => 1
        [SCORE_1] => 6.6490630173025
        [SCORE_2] => 13.010510864225
        [SCORE_3] => 7.5543177306323
        [SCORE_4] => 4.1576775823101
        [SCORE_5] => 5.3696604808987
      )

    [1] => Array
      (
        [SERIAL] => 2
        [SCORE_1] => 11.861835175219
        [SCORE_2] => 0
        [SCORE_3] => 1.988806017442
        [SCORE_4] => 0.97387338276326
        [SCORE_5] => 4.0322016758707
    )

我需要找到其数组中每个元素的比例,例如 SCORE_1/(SCORE_1+...+SCORE_5) 并且序列保持原样。PHP 代码如下所示:

$r = -1;
$namedDataArray = array();

for ($row = 2; $row <= $highestRow; ++$row) {
$dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true,             true, true);
if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
    ++$r;
    foreach($headingsArray as $columnKey => $columnHeading) {

        if($dataRow[$row][$columnKey]==0){

        $namedDataArray[$r][$columnHeading] = 0;

        }elseif($columnHeading=='SERIAL' ){

                $namedDataArray[$r][$columnHeading]=$dataRow[$row][$columnKey];
            } else {
                $x=(float)1.35;
                $a=pow($dataRow[$row][$columnKey],$x);

                $namedDataArray[$r][$columnHeading] = $a;
            }

        }
  }  
}  



echo '<pre>';

print_r($namedDataArray);

echo '</pre><hr />';

我还是 php 新手,一直在努力解决这个问题。我使用 PHPExcel 读取文件并将每个元素提高到 1.35 的幂。我用过$total=array_sum($namedDataArray),但是当我尝试除法时,$Score= $namedDataArray/$total它说不支持的操作数类型。

4

1 回答 1

0

在这条线上:

$Score= $namedDataArray/$total

$namedDataArray是一个数组。PHP 不允许您使用数组进行除法,您必须输入一个数字,例如$Score = $namedDataArray[0]['SCORE_1'] / $total

于 2012-10-05T09:33:40.017 回答