0

I have a simple array $arr which contains 6 numbers.

$arr=[10,24,33,47,58,65];

I want to assign each number to a variable prefixed $color so $color1, $color2 etc up to $color6

This then gives $color1 a value of 10, $color2 a value of 24 etc

Then I want to print each of them out e.g. echo $color1; echo $color2; etc

This is what I'm trying but it doesn't work, is there a better way?

$i=1;

foreach($arr as $row)
{
    $color.$i = implode(",",$row);
    $i++;
}
0

4 Answers 4

4

You're looking for something aptly named variable-variables. To achieve this, you'll need to do:

<?php
    $i=1;
    foreach($arr as $row) {
        ${"color" . $i} = implode(",",$row);
        $i++;
    }
?>

And now you can echo $color1;

Sign up to request clarification or add additional context in comments.

3 Comments

Perfect! Thanks so much, thanks for the link too, a new one on me!
No worries, I wouldn't advise using them mind as they can lead to a lot of confusion later on. A similar thing occurs when using the extract function, if you use an IDE it won't be able to find these variables - but they do remain useful! :) also, if this answer has worked for you, could you press the big tick next to it?
Its for a back up info gathering operation so not in production as such, thanks for the tip.
1

Why don't you just make them as keys and access it as echo $new_arr['color1'];,echo $new_arr['color2'];.... so on ?

<?php
$arr=[1,2,3,4,5,6];
$new_arr = array();
foreach($arr as &$val)
{
    $new_arr["color$val"] = $val;
}
print_r($new_arr);

OUTPUT :

Array
(
    [color1] => 1
    [color2] => 2
    [color3] => 3
    [color4] => 4
    [color5] => 5
    [color6] => 6
)

1 Comment

+1 definitely a use case for an array of related values imho.
1

The next code will work:

$arr = array(10,24,33,47,58,65);
$i=1;

foreach($arr as $row)
{
    $temp = 'color' . $i;
    $$temp = $row; // mention the double $ sign. It will create a variable variable 
    $i++;
}

echo $color1;

http://phpfiddle.org/main/code/fet-sty

Comments

1

As an alternative approach, there's a succinct way to achieve something similar using extract() with the caveat that your variables will be zero-indexed and contain an underscore; e.g: $color_0, $color_1 etc.

$arr = [10, 24, 33, 47, 58, 65];
extract($arr, EXTR_PREFIX_ALL, 'color');

var_dump($color_0, $color_1, $color_2, $color_3, $color_4, $color_5);

Yields:

int 10
int 24
int 33
int 47
int 58
int 65

You can enforce variable naming from one by modifying $arr slightly to enforce an index from one, like so:

$arr = [1 => 10, 24, 33, 47, 58, 65];

This creates variables named $color_1, $color_2 etc.

Hope this helps :)

Edit

I just noticed above, @James' point above is worth noting - a downside of this approach is that you can extract n number of 'invisible' variables into your program's scope. which isn't always a good thing, especially when you have to debug with var_dump(get_defined_vars()). extract can be quite useful though, for instance if you have a simple templating rendering system.

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.