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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献