doushan15559 2014-12-11 06:24
浏览 94
已采纳

PHP变量获取数组值

I'm building a condition check where I'm passing in the variable and a value to check. In this case the variable is an array value, but I can't get it to return it correctly

//happens somewhere else
$specialFeature['option1']="on";
$specialFeature['option2']="on";
$specialFeature['option3']="off";

//what I'm trying to do
#query a db
$row = $result->fetch_array()
#results for purpose of demo
#$row['var'] = "specialFeature['option2']";
#$row['val'] = "on";
if($$row['var'] == $row['val']){
    //what i'm expecting
    echo "OK";
}

My issue is $$var is always null. What am I doing wrong? Is this possible?

  • 写回答

6条回答 默认 最新

  • doushadu0901 2014-12-11 07:18
    关注

    I'm not sure if what I'm doing is "correct", but I got the result I was looking for by doing this:

    //happens somewhere else
    $specialFeature['option1']="on";
    $specialFeature['option2']="on";
    $specialFeature['option3']="off";
    
    //what I'm trying to do
    #query a db
    $row = $result->fetch_array()
    #results for purpose of demo
    #$row['var'] = "specialFeature['option2']";
    #$row['val'] = "on";
    
    $var = $row['var'];
    
    if (strpos($var,'[') !== false) {
        $varA = str_split($var,strpos($var,'['));
        $varA[1] = substr($varA[1],1,-1);
        if (strpos($varA[1],"'") !== false) {
            $varA[1] = substr($varA[1],1,-1);
        }
    }
    
    if(${$varA[0]}[$varA[1]] == $row['val']){
        //what i'm expecting
        echo "OK";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀