PHP - 使用explode创建数组

时间:2013-08-05 22:15:04

标签: php sorting explode

我的php问题如下:

我有这个字符串:$days="Monday* Tuesday* Wednesday*"

我想创建一个这样的数组:$d[1]="Monday"; $d[2]="Tuesday"; $d[3]="Wednesday"

我感觉解决方案经过explode(*, $days),但我无法进一步

感谢您的帮助:)

5 个答案:

答案 0 :(得分:1)

$days="Monday* Tuesday* Wednesday*";
$d=explode('* ', $days);

$days="Monday* Tuesday* Wednesday*";
$d=explode(' ', str_replace('*','',$days));

在最后一个单词的末尾排除“*”。

答案 1 :(得分:1)

explode('* ', trim($days, '*'));

答案 2 :(得分:1)

您应该使用:

explode("* ", $days)

但IMO,我会像这样使用昏迷:

$days  = "Monday,Tuesday,Wednesday";
$days = explode(",", $days);
echo $days[0]; // => Monday
echo $days[1]; // => Tuesday

答案 3 :(得分:0)

$days="Monday* Tuesday* Wednesday*";
$exploded = explode('* ',substr($days, 0, -1));
print_r($exploded);

输出:

Array
(
    [0] => Monday
    [1] => Tuesday
    [2] => Wednesday
)

答案 4 :(得分:0)

<?php
$days = 'Monday* Tuesday* Wednesday*';
$d = array_filter(explode('* ', $days));