0

I have tried to use the search function for a solution to my use case but I wasn't able to find a similar situation like mine. Please kindly help me on this.

I am currently developing a webpage that reads from 2 text files with names and numbers inside into different arrays. For ease of explanation, let's say:

$namesarray = array("x","y");
$valuesarray = array("3","2");

I also have a separate array that carries formulas imported from a text file like this:

$formulaname = array("data1","data2");
$formula = array("x+2","x+y");

So in math terms, the formulas could be written as:

data1 = x+2
data2 = x+y

What I would like to do is to do calculations like this when I call data2:

$data2 = $x + $y;

I'd like php to automatically use 3 and 2 for x and y respectively for the formula although the formula does not have any $ sign to denote x and y as variables.

Would such a situation be possible? I have looked into matheval and eval but I'm not too sure if they could be used for my situation.

Thank you for any assistance. :)

Kurtis Aung
  • 282
  • 4
  • 14

2 Answers2

0

You can use associative arrays, f.e.:

$variables = array('x' => 3, 'y' => 2);
$data1 = $variables['x'] + 2;
$data2 = $variables['x'] + $variables['y'];
fiction
  • 566
  • 1
  • 6
  • 21
  • Thank you for your suggestion. However, the issue I am having is the equation (i.e. x+y) is read from a text file so it's in a string format. Defining every equation is not feasible as there are about 200+ equations for each text file and we have a lot of text files depending on the selection made by the user. – Kurtis Aung Nov 10 '14 at 08:28
  • @user2749222 but.. if you want to use variables - you will also need to define all of them – fiction Nov 10 '14 at 10:56
0
$namesarray = array("x","y");
$valuesarray = array("3","2");
$data = array_combine(
    $namesarray,
    $valuesarray
);
extract($data);

Will create your $x and $y variables with the correct values;

Though using the basic $data array with its x and y keys is probably better, and using eval() is definitely risky if this is user entered formulae

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Thank you for your suggestion. This solves part of my issue of names and values being strings rather than variables. – Kurtis Aung Nov 10 '14 at 08:32
  • However, is there a way for php to automatically recognize something like string formulas such as x+y and use respective values of $x and $y rather than treating the equation as just a string? Thank you for your help again :) – Kurtis Aung Nov 10 '14 at 08:33
  • 1
    Look at the accepted answer to [this question](http://stackoverflow.com/questions/12692727/how-to-make-a-calculator-in-php) – Mark Baker Nov 10 '14 at 08:35
  • The Shunting Yard Algorithm definitely looks interesting but I think it still requires me to have variables in a formula to have php $ sign. As seen in the example, $answer = $math->evaluate('($a + 3) * 4');, it could evaluate equations with 'a' as a variable but only if it has a dollar sign in front. My main issue at hand is that the formulas do not have dollar signs as they are simply read from a text file. Is there any method that could reliably parse formulas with some variables but no $ sign in front to denote them as php variables? Thank you for your time again. – Kurtis Aung Nov 10 '14 at 08:44
  • It only requires variables to have the leading `$` in the formula as written, it's a trivial modification to the regexp to eliminate that.... it should at least provide a starting point for you to work from – Mark Baker Nov 10 '14 at 09:16