How to split the array keyword value separated by

2019-02-28 06:49发布

问题:

I have an array in $_POST:

Array ( [tags] => Javascript,PHP,Java,C++,Python) 

How can i convert this Array into Array like this:

Array ( [tag1] => Javascript [tag2] => PHP [tag3] => Java [tag4] => C++ [tag5] => Python)

I guess i need to use regexp to remove the commas and do split in "foreach as".. But i'm so newbie in PHP... Please help me

回答1:

$tags = explode(",", $_POST['tags']);
print_r($tags);

Outputs

Array (
  [0] => Javascript
  [1] => PHP
  [2] => Java
  [3] => C++
  [4] => Python
)


回答2:

For a string with comma, you can split it using explode

$new_array = explode(',', $_POST['tags']);

Then you can use the $new_array for your process.