PHP:需要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;
}
点赞