Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8186973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:24:22+00:00 2026-06-07T02:24:22+00:00

Okay, so I know that using eval() isn’t great, but I haven’t been able

  • 0

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.

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.

The goal is to take an array of arrays, and turn it into a multidimensional array based on the dimensions in the $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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:24:23+00:00Added an answer on June 7, 2026 at 2:24 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay i know this has been asked a lot but it seems that none
I know that using scriptlets are considered taboo nowadays. Its okay and I will
Okay. I know that I am asking a hackneyed, overchewed question but I am
I know that there have been many similar questions on this topic but none
Solution? Apparently there isn't a faster way, I'm okay with that. I am just
Okay, I know that 1) this is probably not possible with CSS alone and
Okay, here's my short question: I know that === and !== operators will compare
Okay, I know this is a Noob question but I cant see to get
Okay, I know it is weird but when I put this code between <head
I know that buffer overruns are one potential hazard to using C-style strings (char

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.