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

slf4j dependency not found

java maven dependencies slf4j circular-dependency

When trying to deploy my web application, It fails due to logging errors such as the following:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.