0

Sorry if this has already been asked, but it's hard to search for it... I tried googlin this topic without success.

What I want to do is this:

$layoutColor = 2;
$colors1 = array ("F57171", "FACCCC");
$colors2 = array ("FF9900", "FFC66F");

$chosenTheme = "colors".$layoutColor;
echo $chosenTheme [0];

I want to join the $layoutColor variable with the word "colors" in order to get the variable $colors2.

How do I do that?

Thanks.

3
  • You want to treat a string as a name of another variable? Commented May 27, 2013 at 20:15
  • I want to join them both, to get another variable... I used to do this all the time in ActionScript, but I don't how to write it in PHP. I will have a lot of $colors arrays, like $color35, $color36... I wan't to be able to just change the number in the variable $layoutColor, and with that, the variable $chosenTheme will automatically be equal to the colors I want. Commented May 27, 2013 at 20:17
  • 1
    It's called variable variables, and you do NOT want to go there. They lead to utterly unmaintainable code. Use a multidimensional array instead: $colors[1][1] -> FACCCC Commented May 27, 2013 at 20:20

7 Answers 7

4

You're best off approaching this by just combining all your options into a single multi-dimensional array:

$layoutColor = 2;
$colors = array();
$colors[1] = array ("F57171", "FACCCC");
$colors[2] = array ("FF9900", "FFC66F");

$chosenTheme = $colors[$layoutColor];
echo $chosenTheme [0];
Sign up to request clarification or add additional context in comments.

1 Comment

I haven't thought about this, this seems to be the best way to do it indeed. Much better. Thanks.
2

Try this:

<?php
$layoutColor = 2;
$colors1 = array ("F57171", "FACCCC");
$colors2 = array ("FF9900", "FFC66F");

$chosenTheme = "colors".$layoutColor;
echo ${$chosenTheme}[0];

Prints:

FF9900

2 Comments

While I maintain that a multidimensional array is the best approach here, this answer is a more direct answer to the question. +1
Agreed. This works perfectly, but the multidimensional array seems to be the better choice for me. I just didn't thought about it... Thanks anyway!
1

I think you can simplify this using multi dimensional arrays:

$colors = array(
    array ("F57171", "FACCCC"),
    array ("FF9900", "FFC66F")
);

So...

echo $colors[0];

Or you can user variable variables:

$chosenTheme = ${"colors".$layoutColor};

Comments

1

You may try this

$layoutColor = 2;
$$colors2 = "colors".$layoutColor;

So you'll get $colors2 variable

print_r($colors2); // Array ( [0] => FF9900 [1] => FFC66F )

Notice the double $, that will keep the variable name in the variable.

Comments

1

Try with:

$layoutColor = 2;

$string = "color";

echo $$string.$layoutColor;

Comments

0

You need to use dynamic variables $$chosenTheme = "colors".$layoutColor;

then access the array by using this variable $chosenTheme

Comments

0

This might not be an answer to your question but, depending on your specific context, try something like this:

$layoutColor = 2;
$colors = array (
               Array("F57171", "FACCCC"),
               Array ("FF9900", "FFC66F")
           );

echo $colors[$layoutColor][0];

A multi dimensional array is a lot easier to read

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.