0

I have a string like this:

$string = '[miniTrack, boxTrack]'

I want to turn it into an array of variables like this:

$array = [$miniTrack, $boxTrack];

So that I can perform loops and iterations through the variable. And access the variables they are referencing.

I tried eval but I can't figure out how to convert it to a PHP Variable.

I KNOW THAT THIS IS NOT IDEAL, THE STRUCTURE OF THE DATABASE THIS IS PULLING FROM CAN'T BE ADJUSTED UNFORTUNATELY

6
  • 1
    Why you are using [] in strings? Commented Sep 9, 2016 at 16:20
  • Can you use $string = 'miniTrack, boxTrack';? Commented Sep 9, 2016 at 16:21
  • @KajaMydeen No unfortunately Commented Sep 9, 2016 at 16:21
  • 1
    you can't eval. that's not valid PHP code, because minitrack and boxtrack will be seen as undefined/unknown constants. YOu'll have to parse the string manually. explode on commas, strip off the [], etc... And frankly, dynamically generating variable names is just a plain bad idea. it's pretty much always a sign of bad design, and will make for insanely ugly code that's nearly impossible to figure out/maintain later on. Commented Sep 9, 2016 at 16:21
  • 1
    Okay. So you need those values as array? @Jordash Commented Sep 9, 2016 at 16:23

3 Answers 3

1

Your question starts unusually, because you show an array containing a single string that is comma-separated, rather than an array of individual strings.

You could try something like the following:

$arr = [];
$string = ['miniTrack, boxtrack'];
//split the one string into an array, trimming spaces
$vars= array_map('trim', explode(',', $string[0]));
foreach($vars as $var) {
    $arr[] = $var; //add the var to the array 
}

print_r($arr);

Array
(
    [0] => miniTrack
    [1] => boxtrack
)

And if you need to create a variable for each item, you can create "variable variables":

foreach($vars as $var) {
    $my_var = $$var; //variable variable
}
Sign up to request clarification or add additional context in comments.

6 Comments

The $$var part isn't turning it into a variable
it's outputting as blank, but I think this is close
Nevermind I was doing the var wrong, this is working great!
I don't understand the $my_var = $$var; //variable variable though
@EdwardBlack php.net/manual/en/language.variables.variable.php - treats a string as the name of a variable. If $x = 'test'; then $xx would be equivalent to a variable called $test
|
1

It should be as easy as the following:

preg_match_all('~(\w+)~','[miniTrack, boxTrack]', $matches);
foreach($matches[1] as $var)
{
    print $$var;    
}

2 Comments

Why do you print two $$ ?
if $var is miniTrack, $$var will be $miniTrack
0

You can convert your strings to array like this. It may be not ideal but you can use try this.

$string = '[miniTrack, boxTrack]';
$string = str_replace(array('[',']'),array('', '' ), $string);
$array = explode(',',$string);

Here you can iterate your array whatever you want.

Comments

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.