There is a line by type:

user1, user2, user3, user4, user5 

How to create a new variable for each value in the string?

For example:

 $variable = "user1"; $variable = "user2"; 

The string can contain any values ​​separated by commas.

Grateful for any help!

2 answers 2

If necessary, you can still extract ($ users) after the array_combine () function; and then the variables of the same name will appear in the current scope, the values ​​of which will coincide with their names.

 $str = 'user1, user2, user3, user4, user5'; $users = explode(', ', $str); $users = array_combine($users, $users); echo '<pre>'; var_dump($users); echo '</pre>'; /* array(5) { ["user1"]=> string(5) "user1" ["user2"]=> string(5) "user2" ["user3"]=> string(5) "user3" ["user4"]=> string(5) "user4" ["user5"]=> string(5) "user5" } */ 

    As an option.

     $string = "user1, user2, user3, user4, user5"; $array = explode(", ", $string); extract( $array, EXTR_PREFIX_ALL, "my_prefix_"); 

    Original example here .