1

I have searched through many many topics and none have helped me with my specific need as yet.

I tried many options from This Thread but none have been quite right.

I am making a config generator for switches and routers, I am trying to streamline it, so rather than have a config array for each interface, i'll have config array for each type of layout and have the interface name as a variable. I have the web page posting to the script and I have the page posting the port layout per post, with each interface as its own variable.

i.e.

$fa1 = "DESKTOP";
$fa2 = "VOIP";
$fa3 = "DESKTOP";
$fa4 = "";
$fa5 = "DESKTOP";

etc. I am trying to use a foreach loop to go through each interface (as such)

$ints = array($fa1,$fa2,$fa3,$fa4,etc);
foreach ($ints as $line) {
    if ($line == "PC") {
        file_put_contents($file, $pcarray.FILE_APPEND);
    }
    if ($line == "VOIP") {
        file_put_contents($file,$voiparray.FILE_APPEND);
    }
}

with the interface setup as an array:

$pcarray = array(
'interface ' .$interface,
'configetc'
);

I need a function to get the name of the variable for the interface so I can assign it to the $interface variable.

all the examples I've found and tried thus far dont seem compatible with like valued variables.

ie. for the above, most would return fa1 for everything that comes up DESKTOP rather than fa3 for the next desktop one.

an example of the ones that dont quite work how I want is:

function print_var_name($var) {
    foreach($GLOBALS as $var_name => $value) {
        if ($value === $var) {
            return $var_name;
        } 
    } 
    return false;
}

This one, for each variable value repetition after the first just returned the first variables name.

EDIT: What I am wanting to get back would be:

(as the written array)
interface fa1
standard desktop config
interface fa2
standard voip config
interface fa3
standard desktop config
interface fa5
standard desktop config

where as currently im getting:

interface fa1
standard desktop config
interface fa2
standard voip config
interface fa1
standard desktop config
interface fa1 
standard desktop config
2

2 Answers 2

2

I'm not sure if this is what your after, but in the example below, I've just created a foreach() loop to check if the array value from $ints[] is true. If so, then add variable name and value to $varArr array. So the new array would look like ["fa1" => "DESKTOP", "fa2" => "VOIP"]. You then loop through this new array and compare the value using a Switch. Note that $fa4 has been ignored on purpose but you can easily add this in if needs be and test using a default case.

$fa1 = "DESKTOP";
$fa2 = "VOIP";
$fa3 = "DESKTOP";
$fa4 = "";
$fa5 = "DESKTOP";

$ints = [$fa1,$fa2,$fa3,$fa4,$fa5];
$varArr; //Variable Name Array

foreach($GLOBALS as $k=>$v) { //Looping through Globals

    if(in_array($v, $ints) && $v !== "") { //If value is in $ints array, add to this array

        $varArr[$k] = $v;
    }  
}

foreach ($varArr as $k=>$v) { //Create Interface File

    file_put_contents($file, "interface ". $k ."\n", FILE_APPEND);

    switch($v) {
        case "DESKTOP":
            file_put_contents($file, "standard desktop config\n", FILE_APPEND);
            break;
        case "VOIP":
            file_put_contents($file, "standard voip config\n", FILE_APPEND);
            break;
    }    
}

Output:

interface fa1
standard desktop config
interface fa2
standard voip config
interface fa3
standard desktop config
interface fa5
standard desktop config
Sign up to request clarification or add additional context in comments.

3 Comments

I had to add an if block around the file put contents for the interface line to count out all the post values that had been set to globals, but once I did that it worked beautifully, thank you. if($k != "DESKTOP && $k != "VOIP") { file_put contents($file, "Interface ".$k ."\n", FILE_APPEND); } Additionally, due to the interface names being FastEthernet0/X I put a switch block in above this, but it worked exactly as I hoped. thanks again :)
this works nicely, One question, I added the functionality of interface descriptions which add it per interface. Is there a way to have a foreach running two arrays? ie. If its a voip i have a description for the contact number, The contact number is posted as the interface + des (ie $fa02des, $fa03des etc) If its a computer it does not get the description, So I want it to apply the description at the config level suited for that port, (variable is addressed in the $voiparray) or is that too hard and I should just use a switch/case for each port individually?
Hi Craig, If you post another question with example output, I'll be able to take a look for you. I wouldn't say it's impossible or too hard, it's just grasping the logic as to what you need. When posted, just comment my answer with a link to your new question.
1

You can't get variable names the way you're trying to. You should instead pass the list of variable names to look at instead if you need to keep the var names. Then your function that generates your content will just use what you have in the array.

Something simple like this would give you both the value of your variable and its name ($varname is in the foreach loop). (I have no idea where $file, $pcarray and $voiparray come from, so they're just globals in the sample function):

$list = ["fa1", "fa2", "fa3", "fa4", "fa5"];
PutFileContentList($list);
function PutFileContentList(array $list) {
    globals $file, $pcarray, $voiparray;
    foreach ($list as $varname) {
        $line = isset($GLOBALS[$varname]) ? $GLOBALS[$varname] : "";
        if ($line == "PC") {
            file_put_contents($file, $pcarray . FILE_APPEND);
        }
        if ($line == "VOIP") {
            file_put_contents($file, $voiparray . FILE_APPEND);
        }
    }
}

4 Comments

$file is the file its writing to, specified else, didnt think it necessary for this, same for voip and pc arrays, they are just arrays of the configuration file. I will give this a crack, thank you
Tried this, It's not passing the varname through, so $GLOBALS[$varname] is actually searching for a global of $varname, not the actual name of the variable.
The list is a list of the variable names, because you can't get variable names in PHP.
yea i get that, but if i make it echo $GLOBALS[$varname] (within the foreach loop) it just echos 'varname not set' over and over again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.