在PHP中获取变量名称

时间:2012-09-03 10:38:11

标签: php arrays

在下面的代码中,我如何检查$variable是否等于“$ 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
    }   
?>

4 个答案:

答案 0 :(得分:4)

有关详细信息,请访问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
    }   
?>

答案 1 :(得分:1)

简单地说,你不能。您可以在值中添加键:

<?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
    }
}   

答案 2 :(得分:1)

  foreach ($variables as $variable){
        if($variable == $one)//TRUE if $a and $b have the same key/value pairs.
        {

        }
    } 

如果您想检查订单和类型,您可以按照以下步骤进行操作:

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

        }
    } 

答案 3 :(得分:1)

您可以查看

if($variable===$one)

你正在拍摄多维数组。请记住,您需要检查“===”,而不是“==”,因为它不是变量甚至是字符串。