moogeek
moogeek

Reputation: 437

How to split the array keyword value separated by commas in PHP?

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

Upvotes: 1

Views: 2140

Answers (2)

Sampson
Sampson

Reputation: 268324

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

Outputs

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

Upvotes: 1

Donny Kurnia
Donny Kurnia

Reputation: 5313

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.

Upvotes: 1

Related Questions