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
Community
  • 1
  • 1
Craig B
  • 499
  • 4
  • 13
  • Your question is not very clear which is probably why you've had alot of views but no responses. I think I can grasp what your attempting to do though. So to confirm, you do not want duplicate values when assigning the variable name to the `$interface` variable? e.g. if `fa1` has already been assigned for `DESKTOP`, then use `fa3`? – Kitson88 Jan 16 '17 at 08:09
  • yea exactly, the idea is to use one template for all like interfaces and to just assign the interface value each time (inside the foreach loop) so it will do one round with fa1, then fa2 then fa3 but pick up the name of the interface so that it only assigns the interfaces that have a value. ie. if fa5 is not in use but fa6 is a desktop it will just skip over fa5 (that part was working for me,) where as every other method i've found references against the value which makes it keep putting fa1 for each time it comes across a desktop value – Craig B Jan 16 '17 at 20:03

2 Answers2

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
Kitson88
  • 2,889
  • 5
  • 22
  • 37
  • 1
    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 :) – Craig B Jan 17 '17 at 22:00
  • 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? – Craig B Jan 17 '17 at 22:55
  • 1
    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. – Kitson88 Jan 18 '17 at 07:01
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);
        }
    }
}
Mark Ormston
  • 1,836
  • 9
  • 13
  • $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 – Craig B Jan 17 '17 at 01:52
  • 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. – Craig B Jan 17 '17 at 02:07
  • The list is a list of the variable names, because you can't get variable names in PHP. – Mark Ormston Jan 17 '17 at 18:02
  • 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. – Craig B Jan 17 '17 at 21:12