PHP dynamically creating variable to split array

<?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?

Answers


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) :)


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...


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";
?>

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


Need Your Help

Add Original Custom View to the Window

ios iphone objective-c xcode uiview

I added my custom view to the window but it doesn't work. I tried to figure it out but it doesn't work well. (I created this project with the empty project template without using storyboards). This

Size of jdom document vs single variables

java xml jsp session jdom

I planning on reading profile data from a xml file in JSP.