如何在PHP中分割以逗号分隔的数组关键字值?

发布于 2024-08-17 23:58:25 字数 324 浏览 2 评论 0原文

我在 $_POST 中有一个数组:

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

如何将此数组转换为这样的数组:

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

我想我需要使用正则表达式来删除逗号并在“foreach as”中进行分割..但我是 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神爱温柔 2024-08-24 23:58:25
$tags = explode(",", $_POST['tags']);
print_r($tags);

输出

Array (
  [0] => Javascript
  [1] => PHP
  [2] => Java
  [3] => C++
  [4] => Python
)
$tags = explode(",", $_POST['tags']);
print_r($tags);

Outputs

Array (
  [0] => Javascript
  [1] => PHP
  [2] => Java
  [3] => C++
  [4] => Python
)
亚希 2024-08-24 23:58:25

对于带逗号的字符串,可以使用explode进行拆分

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

然后就可以使用$new_array 适合您的流程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文