views:

286

answers:

2

Hello!

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

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.

Donny Kurnia
+1  A: 
$tags = explode(",", $_POST['tags']);
print_r($tags);

Outputs

Array (
  [0] => Javascript
  [1] => PHP
  [2] => Java
  [3] => C++
  [4] => Python
)
Jonathan Sampson
Thank you! It does the thing! :)
moogeek
You're welcome, moogeek. Keep up the great work.
Jonathan Sampson
Btw, it's better to use quotes for accessing array's element, `$_POST['tags']`. Read the documentation, why it is bad write something like `$_POST[tags]`.
Donny Kurnia