1

In the below code, how do I check if $variable equals "$one".

<?php  
    $one = array (1,2,3);
    $two = array (4,5,6);

    $variables = array ($one, $two);

    foreach ($variables as $variable){
        //check if the $variable is equal to "$one"
            //do stuff that is specific for array $one
    }   
?>
Nikola
  • 14,888
  • 21
  • 101
  • 165

4 Answers4

4

For more information visit this

<?php  
    $one = array (1,2,3);
    $two = array (4,5,6);

    $variables = array ($one, $two);

    foreach ($variables as $variable){
        //check if the $variable is equal to "$one"
           if($variable === $one)
            //do stuff
    }   
?>
Uttara
  • 2,496
  • 3
  • 24
  • 35
  • `$three = array (1,2,3); $variables[] = $three;`. That will execute the `// do stuff` twice. – Berry Langerak Sep 03 '12 at 10:46
  • 2
    if this is really what the OP wants, the question should be closed because it's not a real question asking how to do an IF block. – Gordon Sep 03 '12 at 10:47
1

Simply put, you can't. You can add a key to the values though:

<?php  
$one = array (1,2,3);
$two = array (4,5,6);

$variables = array ( 'one' => $one, 'two' => $two);

foreach ($variables as $key => $variable){
    //check if the $variable is equal to "$one"
    if( $key === 'one' ) {
        //do stuff that is specific for array $one
    }
}   
Berry Langerak
  • 18,561
  • 4
  • 45
  • 58
1
  foreach ($variables as $variable){
        if($variable == $one)//TRUE if $a and $b have the same key/value pairs.
        {

        }
    } 

And if you want to check for order and types as well you can do as follow:

  foreach ($variables as $variable){
        if($variable === $one)
        {

        }
    } 
Tarun
  • 3,162
  • 3
  • 29
  • 45
1

You can check with

if($variable===$one)

You are taking multi dimensional array. Keep in mind and you need to check with "===", not with "==" because its not an variable or even a string.

Bart
  • 19,692
  • 7
  • 68
  • 77
GautamD31
  • 28,552
  • 10
  • 64
  • 85