0

I have two arrays. First contains variable names and second contains both float values and condition extracted from MySQL table with data type varchar. Now i want to use these names and values in if statement. Is it possible? Let me explaine the code.

for ($i=0; $i<5; $i++) {
    if ($_POST[$names[$i]] $val[$i]) {
        echo "Do something";
    }                       
}

$names = array (name1, name2, name4 )
$val = array (<121.5, <=88.9, >77.3)

I want to perform logical comparison between these two and the comparison operators are stored with values in an array you can see. if it is not possible with if statement. kindly suggest me other method to do it. Thanks!

8
  • yes, please do explain the code. what exactly are you trying to do? do you want to compare the values? then place a comparing operator between them. Commented Oct 22, 2016 at 17:16
  • It is unclear what you actually want to achieve with this strange approach. Commented Oct 22, 2016 at 17:16
  • I'd use the values in your query so you only get the records you want. Question needs to be clearer though. Commented Oct 22, 2016 at 17:17
  • @FranzGleichmann yeah i want to apply <, <=, > condition between these two but these conditions are already in there with numbers as you can see in $val array i had mention. Commented Oct 22, 2016 at 17:22
  • @chris85 the conditions are there with values you could see in $val array i had mention. i want to get number with condition attached with it. Commented Oct 22, 2016 at 17:25

2 Answers 2

0

I would suggest you to do it other way. I know it looks crazy, but it will solve your problem :)

$names = array (name1, name2, name4 );
$val = array (array('bigger',121.5), array('smaller_even',88.9),array('smaller',77.3));

for ($i=0; $i<5; $i++){
    switch ($val[$i][0]){
        case: 'bigger':
        if($name[$i]>$val[$i][1]){
           echo 'do something';
        }
        break;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

These are the rules which i m getting from MySQL table and each attribute have both values and comparison operator stored in every attribute. so i can't replace the operators with this.
So if it is stored in database first you need to check what type of comparison is it. You can try to do it with strpos() (remeber to first check out longer versions - "<=" and ">="). If(strpos($val[$i],'<=')!==false){echo 'do something'} elseif (...)
0

Here's an idea:

First, use this regexp on your comparison array $val:

preg_match('/([^\d]+)(\d+)/', $val[$i][0], $match);

Now, you'll have the comparison operator in $match[1] and the number to compare to in $match[2]. Then use a few ifs or a switch to check which comparison operator it is, and then use a regular if clause.

For example:

if ($match[1] === '<=') {
    if ((int)$match[2] <= $_POST[$names[$i]]) {
        //do something
    }
} elseif ($match[1] === '>') {
    if ((int)$match[2] > $_POST[$names[$i]]) {
        //do something else
    }
}

2 Comments

Can you please elaborate this?
@tooba Sure, I added an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.