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 3441726
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:35:17+00:00 2026-05-18T08:35:17+00:00

I would like to write a function that will process formulas that are comprised

  • 0

I would like to write a function that will process formulas that are comprised of array keys. For example, (1+3+5+7-8)*9 or (1-3+5)/6 and etc Any suggestions?

EDIT:
I think it’s better to think of this as an excel grid, with field coordinates being the array keys and one of the fields being the formula field (e.g. A1+A2-B2*B3)

  • 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-05-18T08:35:18+00:00Added an answer on May 18, 2026 at 8:35 am
    
    
    function convert_formula($formula, $formula_array) {
        $callback = function($matches) use ($formula_array) {return $formula_array[(int) $matches[0]];};
        $converted = preg_replace_callback("/[0-9]+/", $callback, $formula);
        return eval("return $converted;");
        }
    
    $test_formula = '(3-1) * (3-10)';
    $test_formula_array = array(323,67,82,56, 10 => 3);
    
    echo convert_formula($test_formula, $test_formula_array);
    
    
    
    

    Requires PHP 5.3.+ for anon functions. You’ll want to first validate the $formula argument to ensure it accepts only numbers and math symbols before you eval it within the function.

    Edit: just going by some assumption that this is a tool to instruct students on math and that you already have a pre-built set of formulas to reference… as eval is one of the most dangerous functions of PHP, perhaps you might consider running this client side using javascript. When you build the page, you simply dynamically create a base reference javascript array containing all possible values to be used in the formula. So this way, using javascript’s eval function, the client builds and resolves the formula all on their end. That final evaluation is then passed back to your server which then validates the evaluation and gives a final “correct/wrong” response. Just a thought…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write an algorithm that compares one memory block with another
Lets say we have a native library that works with data like this: double
I would like to implement an API for my web based service. I've been
I am using Ruby on Rails 3 and I would like to use jQuery
is there are in build php function or I have to write my own
Let's say I have to read from a directory that has many large XML
Sometimes I write projects and don't return to them until months later. Unfortunately for
I am trying to write a SQL query to fetch rows whose fields contain
Is it possible in C++ to have two classes, let's call them A and
I'm using ASP.NET with C# 2.0. I have created some objects for a database

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.