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

时间:2021-11-21 21:33:22

Okay, so I know that using eval() isn't great, but I haven't been able to come up with a better solution to my problem, and until recently, there wasn't a performance reason not to use it. However, I am now passing enough data to the function that it is taking unacceptably long.

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

The function that is being called is:

被调用的函数是:

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

It is being called like this:

它被称为这样:

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

And the variables being passed to the function are similar to:

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

$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");

So I want to rewrite the function so that I'm not using eval() any more. I've spent a considerable amount of time on this, and feel that it's time to seek some advice.

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

The goal is to take an array of arrays, and turn it into a multidimensional array based on the dimensions in the $dimensionArray.

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

I don't want to bore you with too many details right now, so please ask if you need more or have any other questions

我现在不想向你提供太多细节,所以请问你是否需要更多或有任何其他问题

2 个解决方案

#1


3  

Wow, okay. First time through I missed your indexing concatenation. Try this:

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

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

#2


-1  

You should look at create_function() (docs here)

你应该看看create_function()(这里是docs)

#1


3  

Wow, okay. First time through I missed your indexing concatenation. Try this:

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

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

#2


-1  

You should look at create_function() (docs here)

你应该看看create_function()(这里是docs)