开发者

PHP: Splitting by ", " and show

开发者 https://www.devze.com 2023-01-19 11:28 出处:网络
$number = 1; $number2 = \"2, 3, 4\"; $searchstatus = array(\" - \", \"f1\", \"f2\", \"f3\", \"f4\"); I have this array.
$number = 1;
$number2 = "2, 3, 4";
     $searchstatus = array(" - ", "f1", "f2", "f3", "f4");

I have this array.

$number2 is like how开发者_开发问答 its stored in the database.

How can i split them up by ", " if theres more than 1(like $number) and make a echo ouput that says:

f2 and then f3 and then f4

(values to 2, 3, 4)

So if its $number just echo "f1" , if theres more than 1 "entry" like $number2, echo like above^


Your question is a bit confusing, please clarify.

In the meantime, I think you are looking for explode() and implode().


Use something like:

$searchstatus = array(" - ", "f1", "f2", "f3", "f4");
$arr = array_map('trim', explode(',', '1,2,3'));
foreach ($arr as $key) {
    echo $searchstatus[$key] . " ";
}

Prints:

f1 f2 f3


If the spacing around your commas is inconsistent, you'll want to use preg_split():

$list = preg_split('/\s*,\s*/', $string);


You mean that you have a string Like this

$str='1,2,3.......n';

Use PHP explode function that is as

$exp=explode(','$str);

It will split the string into any array of pieces like:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [n] => 3
)

If you want convert the array to string then use PHP implode()

implode(',',$arr);

For white spaces removal from left and right of the values then use trim().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号