Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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()
share|improve this question
 
Try changing ${$supera} to $supera –  sush 19 mins ago
 
thx.. was a typo.. already saw it –  user26676 19 mins ago
 
according to your array, you will not need two foreach, also you haven't shown keys in $supera array –  PravinS 18 mins ago
 
@PravinS no, they are existing data filled arrays EG: $RowService['my_column'] already exists as does $RowService['my_other_column'] and $RowSite['different_column'] etc etc multiplied by about 100 –  user26676 16 mins ago
 
Gather your data into an array, and iterate over it w/out variable variables - even more brilliant. –  moonwave99 11 mins ago
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
 
yes you're right adding if(is_array(${$super})) in front of the nested foreach did it –  user26676 1 min ago
add comment

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;
share|improve this answer
 
you're right, using pointers &$ is probably the right way to do it, I am very familiar with them and you are right. –  user26676 14 mins ago
 
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 4 mins ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.