PHP:需要一个 eval() 的替代方法来动态构建多维数组

标签 php arrays eval

好吧,我知道使用 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 中的维度将其转换为多维数组。

我现在不想让你厌烦太多细节,所以如果你需要更多或有任何其他问题,请询问

最佳答案

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

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;
}

关于PHP:需要一个 eval() 的替代方法来动态构建多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11352669/

相关文章:

php - 为订单添加固定折扣值

javascript - 通过另一个 true 标记的子键对象过滤对象数组

c# - 对 DBNull 的评估检查不起作用

php - 也许 PHP eval 范围的事情?

php - 如何获取插入记录时的ID?

PHP PDO UPDATE MySQL 多个表

php - 将对象转换为数组

c - 将txt文件中的测验问题放入c中的数组

c++ - 你如何在 C++ 中拆分数组?

javascript - iOS 使用 UIWebView 使用 Math.pow 执行 JavaScript 字符串