0
<?php
    $my_array = array("hello","world","howareu");
    $c = count($my_array);

    for($i=0;$i<=$c;$i++){
        $v = '$var'.$i;
        $splited = list($v) = $my_array;
    }
?>

input: $my_array

But expected output:

if I echo $var0, $var1, $var2;

hello, world, howareu

How to create dynamic PHP variables based upon the array count and then convert them into a list as a string?

5
  • If anyone isn't clear about this, let me know so that I can post some more inputs. Commented Dec 9, 2013 at 10:56
  • Please, add more info. Not clear what you really need. Add input and output you want to receive Commented Dec 11, 2013 at 14:17
  • But where in your examples are you using the echo $splited. Also, the documentation supplied tells us that list() is returning the defined array. Echo'ing an array is not possible, use var_dump instead to see the content. the var_dump ( iterated 4 times due to the <= in the for-loop ) is showing the same content as it is actually the $my_array every time. Commented Dec 11, 2013 at 14:35
  • @kovpack - Please check the revised post. Commented Dec 11, 2013 at 14:35
  • Take a look on my answer. It seems to be what you need. Variable variables is what you need. Commented Dec 11, 2013 at 14:51

4 Answers 4

0

You do not need list for that. $$ will suit you perfectly.

$my_array = array("hello", "world", "howareu");

foreach ($my_array as $key => $val)
{
    $a = 'var'.$key;
    $$a = $val;
}

echo $var0,", ", $var1,", " $var2;

Take a look here - Variable variables

Added: or if you need count and for

for ($i = 0; $i < count($my_array); $i++)
{
    $a = 'var'.$i;
    $$a = $my_array[$i];
}

echo $var0,", ", $var1,", " $var2;

Of course, this line echo $var0,", ", $var1,", " $var2; sucks and looks like crap :) But in order to receive EXACTLY what you want, you need to modify variables, output like I've wrote, or use some function like implode with grue ', '.

Updated:

But if you need just that output, why not to use simple implode(', ', $my_array) :)

10
  • Yes, it's working good but I don't want the $var0 till $var2 not static but dynamic because we will not be knowing the number of data present in the array. So how we can make it like all 0,1,2 in a variable inside the foreach... Commented Dec 11, 2013 at 15:10
  • ..but what variable to implode Commented Dec 11, 2013 at 15:19
  • You task seems completely strange now :) You WILL know the number of data in an array - you can always use count($array) to get the number of elements. But I've got question: why do you refuse to use associative array or just array for that? Commented Dec 11, 2013 at 15:21
  • And I've got the most important question: what your code is intendef for? :) I'm sure there are better ways to do that than the one offered above. Always use the simplest code. Commented Dec 11, 2013 at 15:24
  • implode on the actual array, having the ', ' as your glue parameter will do the same: echo implode(', ', $my_array); Commented Dec 11, 2013 at 15:24
0

it's a matter of the data you need to process...if it's pretty static, you don't need the second foreach() for example, since you compare the keys anyways...

foreach($datavalueas $resultdatakey=>$resultdatavalue){
    if($resultdatakey== 'A'){
    //stuff for a
    }
    if($resultdatakey== 'B'){
    //stuff for b
    }
}

would become

if(isset($datavalueas['A'])){
    //stuff for a
}
if(isset($datavalueas['B'])){
    //stuff for b
}

since the foreach uses copies of the array, which are pretty bad for the performance...

0
0

Assuming i got your question right, you could use something like:

$array = array( 'x', 'y', 'z' );
foreach ($array as $name )
    $$name = rand(1,100);

var_dump($x); 

the $$ is key here, the first $ implies the variable as the second $ is used as the identifier for the variable. In this case the value being iterated over in the array. Giving us 3 variables: $x, $y, $z.

-- edit:

the correct code, besides using extract():

<?php
    $my_array = array("hello","world","howareu");
    $c = count($my_array);

    for($i=0;$i < $c;$i++){
        $v = 'var'.$i;
        $$v = $my_array[$i];
    }

    echo "$var0, $var1, $var2";
?>
4
  • And ofcourse you can use an extra $i to make the name for the variable, instead of using the value in the array. Commented Dec 11, 2013 at 14:18
  • It is returning Notice: Undefined variable: var0, var1 & var2 with printed , , Also remember that, I would be needing like not directly or statically or hardcoded as $var0, $var1 and $var2 but $var.$i. Commented Dec 11, 2013 at 14:42
  • Remove the $ from the $v = '$var'.$i; I forgot it in my supplied code. Commented Dec 11, 2013 at 14:45
  • Then repost your updated code, it is compiling and working as expected if i run my code. The code of kovpack is now exactly the same as mine btw:) Commented Dec 11, 2013 at 14:54
0

You can create dynamic variables via variables variable as Mr.kovpack have stated here. In below code you can access the variables from 0 to $c-1(count of array) as per your comment to Mr.kovpack.

$my_array = array("hello","world","howareu");
    $c = count($my_array);

    for($i=0;$i<=$c-1;$i++){
        ${'var'.$i} = $my_array[$i]; //Dynamic variable creation
//        $splited = list($v) = $my_array;
    }
echo $var0.$var1.$var2;

or you could use like below:

$my_array = array("hello","world","howareu");
    $c = count($my_array);

    for($i=0;$i<=$c-1;$i++){
        $a='var'.$i;
        $$a = $my_array[$i];
    }
echo $var0."-".$var1."-".$var2;

You can read more on it here

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.