用逗号和字符串PHP分隔数组 [英] Separate an array with comma and string PHP

查看:69
本文介绍了用逗号和字符串PHP分隔数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用逗号和单词分隔一个数组.

I want to separate an array with comma and word.

这是我编写的代码:

$array = array( 'user1', 'user2', 'user3', 'user4', 'user5' );
$last = array_pop( $array );
$string = count( $array ) ? implode( ", ", $array ) . " and " . $last : $last;

printf( 'Authors: %s', $string );

我的代码打印此:

作者:user1,user2,user3,user4和user5

Authors: user1, user2, user3, user4 and user5

我成功编写了一个代码,用逗号将数组内插,然后搜索最后一个键,并用单词"and"将其分开.

I successfully made a code to implode the array with a comma and search for the last key and separate it with a word 'and'.

但这就是我想要的,但是经过两天以上的努力,我失败了:

But that's what I want but I failed after more than 2 days of work on this:

作者:user1,user2和user3以及另外2个.

Authors: user1, user2, and user3 and 2 others.

推荐答案

虽然我一直在寻找用于处理此类任务的智能计算流程,但我将提供更多行人替代方案.(出于记录,我讨厌写了一堆 if-elseif-else 条件,几乎等于我讨厌 switch-case 的冗长程度>块.对于这个狭窄的任务,我选择放弃我喜欢的数组函数,并使用其自定义的定界符有条件地打印这些值.

While I always look for the clever calculated process for handling a task like this, I'll offer a much more pedestrian alternative. (For the record, I hate writing a battery of if-elseif-else conditions almost as much as I hate the verbosity of a switch-case block.) For this narrow task, I am electing to abandon my favored array functions and conditionally print the values with their customized delimiters.

尽管此解决方案在页面上的可扩展性最低,但它可能是最容易理解的(将代码与其生成的输出相关联).如果这是关于喜欢"的话,或某些方面,我对扩展性的需求并不高-但我可能是错的.

While this solution is probably least scalable on the page, it is likely to be the easiest to comprehend (relate the code to its generated output). If this is about "likes" or something, I don't really see a large demand for scalability -- but I could be wrong.

请密切注意2位数情况.我的解决方案使用而不是来分隔元素,似乎更适合英语/人类.

Pay close attention to the 2-count case. My solution delimits the elements with and instead of , which seems more English/human appropriate.

代码:(演示)

$arrays[] = array();
$arrays[] = array('user1');
$arrays[] = array('user1', 'user2');
$arrays[] = array('user1', 'user2', 'user3');
$arrays[] = array('user1', 'user2', 'user3', 'user4');
$arrays[] = array('user1', 'user2', 'user3', 'user4', 'user5');

foreach ($arrays as $i => $array) {
    echo "TEST $i: ";
    if (!$count = sizeof($array)) {
        echo "nobody";
    } elseif ($count == 1) {
        echo $array[0];
    } elseif ($count == 2) {
        echo "{$array[0]} and {$array[1]}";
    } elseif ($count == 3) {
        echo "{$array[0]}, {$array[1]}, and {$array[2]}";
    } else {
        echo "{$array[0]}, {$array[1]}, {$array[2]}, and " , $count - 3, " other" , ($count != 4 ? 's' : '');
    }
    echo "\n---\n";
}

输出:

TEST 0: nobody
---
TEST 1: user1
---
TEST 2: user1 and user2
---
TEST 3: user1, user2, and user3
---
TEST 4: user1, user2, user3, and 1 other
---
TEST 5: user1, user2, user3, and 2 others
---


p.s.相同处理方式的更紧凑版本:


p.s. A more compact version of the same handling:

代码:(演示)

if (!$count = sizeof($array)) {
    echo "nobody";
} elseif ($count < 3) {
    echo implode(' and ', $array);
} else{
    echo "{$array[0]}, {$array[1]}";
    if ($count == 3) {
        echo ", and {$array[2]}";
    } else {
        echo ", {$array[2]}, and " , $count - 3, " other" , ($count != 4 ? 's' : '');
    }
}


最后,这是一种医治"医生的方法.数组并将附加到最后一个元素.我认为无论如何旋转此任务,都会有一定程度的卷积.


And finally, here's an approach that "doctors up" the array and prepends and to the final element. I think anyway you spin this task, there's going to be a degree of convolution.

代码:(演示)

$count = sizeof($array);
if ($count < 3) {
    echo implode(' and ', $array);
} else {
    if ($count == 3) {
        $array[2] = "and {$array[2]}";
    } else {
        $more = $count - 3;
        array_splice($array, 3, $more, ["and $more other" . ($more > 1 ? 's' : '')]);
    }
    echo implode(', ', $array);
}


在CodeReview期间,我意识到"pop-prepend-push"技术: https://codereview.stackexchange.com/a/255554/141885

这篇关于用逗号和字符串PHP分隔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆