简体   繁体   English

逗号分隔的列表到 PHP 中的非关联数组

[英]Comma separated list into a non associated array in PHP

I have a string of emails:我有一串电子邮件:

$emails = 'email1@example.com, email2@example.com,email3@example.com';

and I need a basic array like [one,two,three] and not [0 => 'one', 1 => 'two', 2 => 'three'] .我需要一个像[one,two,three]这样的基本数组[one,two,three]而不是[0 => 'one', 1 => 'two', 2 => 'three'] I guess is a non-associative array?我猜是一个非关联数组?

How to do that?怎么做? I have tried the inbuild 'explode()' PHP function but it creates 0 => one, ... instad of aa simple array email1,email2,..我已经尝试了 inbuild 'explode()' PHP 函数,但它创建了 0 => one, ... 一个简单数组 email1,email2,..

Besides, explode has problem when there is a space after the comma, so I have to use ', ' or ',' but can't use both.此外,当逗号后有空格时,explode 会出现问题,所以我必须使用', '','但不能同时使用两者。

What PHP function is the best to convert a list of email addresses into a simple non-associative array?什么 PHP 函数最适合将电子邮件地址列表转换为简单的非关联数组? Thanks in advance.提前致谢。

First you can remove spaces by str_replace首先,您可以通过str_replace删除空格

$emails = str_replace(' ', '', $emails);

and I need a basic array like [one,two,three] and not [0 => 'one', 1 => 'two', 2 => 'three'].我需要一个像 [one,two,three] 这样的基本数组,而不是 [0 => 'one', 1 => 'two', 2 => 'three']。 I guess is a non-associative arr我猜是一个非关联的 arr

In php simple arrays has numeric keys, so it's correct在 php 中简单数组有数字键,所以它是正确的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM