3

好的,所以我知道使用 eval() 不是很好,但是我无法为我的问题提出更好的解决方案,而且直到最近,还没有不使用它的性能原因。但是,我现在将足够的数据传递给函数,它花费的时间长得令人无法接受。

被调用的函数是:

public static function makeAMultiDimensionalArrayWithSumsBasedOnMultipleFields($inArray, $dimensionFieldNames, $sumFieldNameArray, $staticFieldNameArray = array())
{
    $outArray = array();

    // Just in case the array has indices, sort it so array_pop works as expected.
    ksort($dimensionFieldNames);

    foreach ($inArray as $row)
    {
        // make sure each row in the inArray has all keys specified by $dimensionFieldNames
        $allFieldsPresent = TRUE;
        foreach ($dimensionFieldNames as $keyFieldName)
        {
            if (!array_key_exists($keyFieldName, $row))
            {
                // Note that alternatively we could set the field to a specified default value.
                $allFieldsPresent = FALSE;
            }
        }

        if ($allFieldsPresent)
        {
            $indexString = '';
            $keyFieldNameArrayCopy = $dimensionFieldNames;

            foreach ($dimensionFieldNames as $keyFieldName)
            {
                $indexString .= "['" . $row[$keyFieldName] . "']";
                // lets sum values
                foreach ($sumFieldNameArray as $sumFieldName)
                {
                    eval ('$outArray' . $indexString . '[' . $sumFieldName . '] += $row[' . $sumFieldName . '];');
                }

                foreach ($staticFieldNameArray as $staticFieldName)
                {
                    eval ('$outArray' . $indexString . '[' . $staticFieldName . '] = $row[' . $staticFieldName . '];');
                }
            }
        }

    }
    return $outArray;
}

它是这样调用的:

makeAMultiDimensionalArrayWithSumsBasedOnMultipleFields($data, $dimensionArray, $sumArray, $staticArray);

传递给函数的变量类似于:

$dimensionArray = array("firstLevelID", "secondLevelID", "thirdLevelID", "fourthLevelID", "fifthLevelID");
$sumArray = array("revenue", "cost", "profit", "sales", "inquires", "cost", "walkins");
$staticArray = array("date", "storeID", "storeName", "productID", "productName", "managerID", "managerName", "salesperson");

所以我想重写这个函数,这样我就不再使用 eval() 了。我在这方面花了相当多的时间,觉得是时候寻求一些建议了。

目标是获取一个数组数组,并根据 $dimensionArray 中的维度将其转换为多维数组。

我现在不想用太多细节让你厌烦,所以请询问你是否需要更多或有其他问题

4

2 回答 2

3

哇,好吧。我第一次错过了您的索引连接。试试这个:

if ($allFieldsPresent) {
    $keys = array();
    foreach ($dimensionFieldNames as $keyFieldName) {
        $keys[] = $row[$keyFieldName];
        // lets sum values
        foreach ($sumFieldNameArray as $sumFieldName)
            self::deepAssign($outArray, $keys, $sumFieldName, $row[$sumFieldName], true);
        foreach ($staticFieldNameArray as $staticFieldName)
            self::deepAssign($outArray, $keys, $staticFieldName, $row[$staticFieldName]);
    }
}

protected static function deepAssign(&$array, $keys, $fieldName, $value, $sum = false) {
    $target =& $array;
    foreach ($keys as $key) {
        if (!isset($target[$key]))
            $target[$key] = array();
        $target =& $target[$key];
    }
    if($sum)
        $target[$fieldName] += $value;
    else
        $target[$fieldName] = $value;
}
于 2012-07-05T21:19:03.550 回答
-1

您应该查看create_function()(此处的文档

于 2012-07-05T21:17:20.743 回答