0

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){
user26676
  • 280
  • 3
  • 21

3 Answers3

1

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.

slash197
  • 9,028
  • 6
  • 41
  • 70
  • yes you're right adding `if(is_array(${$super}))` in front of the nested foreach did it – user26676 Mar 20 '14 at 10:48
  • by the way it's a helluva trick this.. means that umpteen print forms can print/cough-up the field names they use as opposed to the data inside. massive win for such a little thought. means users aren't guessing what field is what, i.e. they can hit a help button. so code is not effected – user26676 Mar 20 '14 at 10:50
0

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;
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • you're right, using pointers `&$` is probably the right way to do it, I am very familiar with them and you are right. – user26676 Mar 20 '14 at 10:34
  • the first one didn't work - bombed with the same error message - must be a PHP failure within foreach logic or (however it just occured to me, following a completely DIFFERENT line of thought) **perhaps** it's a misunderstanding - it's a consequence of me assigning the `$val[$key]` while in a loop – user26676 Mar 20 '14 at 10:44
0
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);
payal
  • 36
  • 2