echo list of variables into array

I’m trying to assign an array to “$dataar” like this:

[php]$dataar[]=array($dataarstring);[/php]
The only problem is I want the “$dataarstring” to be a list of variables, specifically other arrays. I want whatever is in the “$dataarstring” to be echoed so the result is something like this:

[php]$dataar[]=array($ifdescar[$theid],$ifaliasar[$theid],$ifadminstatusar[$theid]);[/php]
The list of variables changes. Instead of having to create tons and tons of if statements for different conditions of variables, I’d like to just add a list of my array variables to “$dataarstring” then echo it inside that first line of code. Is this possible?

I’d populate “$dataarstring” like this:

[php]$dataarstring=’$ifdescar[$theid],$ifaliasar[$theid],$ifadminstatusar[$theid]’;
//Some other array needs to be added
$dataarstring=$dataarstring . ‘,$ifoperstatusar[$theid]’;[/php]

When “$dataarstring” is echoed inside the first line I can’t figure out how to get PHP to treat it like a list of variables instead of just a text string.

Take a look at the array_map() function.

Tried array_map and that didn’t work for me. This works when I manually specify the variables inside the array:

[php]foreach($ifdescar as $theid => $ifdesc){
$dataar[]=array($ifdescar[$theid],$ifaliasar[$theid],$ifadminstatusar[$theid],$ifoperstatusar[$theid],$ifspeedar[$theid],$ifduplexar[$theid]);
}
print_r($dataar);[/php]
This is the output (WORKS):

http://i445.photobucket.com/albums/qq178/quicky2g/working_example_zps9450c098.png

Not sure if I used array_map correctly, but this is what I did:

[php]$dataarstring=array(’$ifdescar[$theid]’,’$ifaliasar[$theid]’,’$ifadminstatusar[$theid]’,’$ifoperstatusar[$theid]’,’$ifspeedar[$theid]’,’$ifduplexar[$theid]’);
foreach($ifdescar as $theid => $ifdesc){
$dataar[]=array_map($dataarstring);
}
print_r($dataar);[/php]
This is the output (DOES NOT WORK):

http://i445.photobucket.com/albums/qq178/quicky2g/broken_example_zpsfea972ac.png

Also tried this and it didn’t work:

[php]$dataarstring=’$ifdescar[$theid],$ifaliasar[$theid],$ifadminstatusar[$theid],$ifoperstatusar[$theid],$ifspeedar[$theid],$ifduplexar[$theid]’;
foreach($ifdescar as $theid => $ifdesc){
$dataar[]=eval(‘array(’ . $dataarstring . ‘);’);
}
print_r($dataar);[/php]
Any other ideas? Thanks in advance!

Did you happen to notice a difference in how the variables are listed in the code that works and the one you say does not?

Not sure if this is the technically correct way to accomplish this, but a friend of mine suggested this and it works:

[php]$dataarstring=’$ifdescar[$theid],$ifaliasar[$theid],$ifadminstatusar[$theid],$ifoperstatusar[$theid],$ifspeedar[$theid],$ifduplexar[$theid]’;
foreach($ifdescar as $theid => $ifdesc){
eval(’$dataar[] = array(’ . $dataarstring . ‘);’);
}
print_r($dataar);[/php]
I get working results now!

Sponsor our Newsletter | Privacy Policy | Terms of Service