duanhuilao0787
2014-03-20 10:27 阅读 87
已采纳

foreach循环中的PHP变量变量不起作用

This is a brilliant little trick if I can get it to work - I have hundreds data columns from dozens of tables spread across a dozen data forms (they are HTML print forms) and they are all html with embedded php variables. Very normal. However the customer had a requirement to know what field went in where - a very good question.

So what did I do? I worked on a solution that allows the key'd arrays from the database to give up their column names. a brilliant move! except I need to do it via variable variables, and guess what, they DON'T work in a foreach loop.

here is the code

   if ($_REQUEST['data']=="false"){
        $supera = array("RowService", "RowSite", "RowCustomer", "RowEngineer"); //there can be many of these they are key'd arrays $RowService['column_name_1']; is the format
        foreach($supera as $super){
            foreach(${$super} as $key=>$value){
                if (!is_numeric($key)){
                    ${$super}[$key] = "<span style=\"color:pink;\">".$key."</span>";
                }
            }
        }
    }

as you can see I want a kill switch easy mechanism to cut and paste the key'd arrays that aren't to show real data any more rather they are to show (in pink) the column name, and (perhaps) the table name too. There is a lot of code already in place and this would be a brilliant option if it can be made to work

EDIT: this is the PHP error:

 Warning: Invalid argument supplied for foreach()

EDIT: THE CODE ACTUALLY ALREADY WORKS: FIX IS TO test for is_array()

 if(is_array(${$super})) foreach(${$super} as $key=>$value){

will work, as opposed to just

 foreach(${$super} as $key=>$value){
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dongpian6319 dongpian6319 2014-03-20 10:38

    I'm not sure what you are trying to achieve but your code (simplified) works just fine:

    $a = array("asd", "qwe");
    $asd = array("a" => 1, "b" => 2, "c" => 3);
    $qwe = array("d" => 4, "e" => 5, "f" => 6);
    
    foreach ($a as $item)
    {
        foreach ($$item as $key => $value)
        {
            echo $key . ": " . $value . "<br />";
        }
    }
    

    Output:

    a: 1
    b: 2
    c: 3
    d: 4
    e: 5
    f: 6
    

    Most likely one of your variables is empty (not an array) and that's why you receive that warning.

    点赞 评论 复制链接分享
  • dpc57092 dpc57092 2014-03-20 10:33

    Personally, I find variable variables to be a really bad idea. There are a few ways around it.

    For example:

    $process = array(&$RowService,&$RowSite,&$RowCustomer,&$RowEngineer);
    foreach($process as $p) {
        foreach($p as $k=>$v) {
            $p[$k] = "<span style=\"color:pink\">".$v."</span>";
        }
    }
    

    Using references means you can affect the original variables.

    If the above doesn't work (I'm not that great with references XD), try this:

    $process = array($RowService,$RowSite,$RowCustomer,$RowEngineer);
    foreach($process as $p) {
        foreach($p as $k=>$v) {
            $p[$k] = "<span style=\"color:pink\">".$v."</span>";
        }
    }
    list($RowService,$RowSite,$RowCustomer,$RowEngineer) = $process;
    
    点赞 评论 复制链接分享
  • dongzhi6463 dongzhi6463 2014-03-20 11:34
    As per my understanding of your requirement.
    
    If you want to get table name with pink color then you just need to use below code 
    
    $supera = array("RowService", "RowSite", "RowCustomer", "RowEngineer"); //there can be many of these they are key'd arrays $RowService['column_name_1']; is the format
    $super = array();
            foreach($supera as $key=>$value){
                    if (!is_numeric($value)){
                        $super[$value] = "<span style=\"color:pink;\">".$value."</span>";
                    }
            }
            print_r($super);
    
    点赞 评论 复制链接分享

相关推荐