Php 用逗号和字符串分隔数组

Php 用逗号和字符串分隔数组,php,arrays,implode,Php,Arrays,Implode,我想用逗号和单词分隔数组 这是我编写的代码: $array = array( 'user1', 'user2', 'user3', 'user4', 'user5' ); $last = array_pop( $array ); $string = count( $array ) ? implode( ", ", $array ) . " and " . $last : $last; printf( 'Authors: %s', $string ); 我的代码打印如下: 作者:user1、u

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

这是我编写的代码:

$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

我成功地编写了一个代码,用逗号对数组进行内爆,搜索最后一个键,并用单词“and”将其分隔开

但这正是我想要的,但经过两天多的努力,我失败了:


作者:user1、user2、user3和其他两种。

有几种方法可以做到这一点,一种简单的方法是:

首先,您应该获取数组中的元素数,看看它是否超过3

$count = count( $array) ;
然后,如果大于3,则可以创建字符串:

if($count>3){
    $string= $array[0] . 'and' . $array[1] . 'and' . $array[2] . 'and' . ($count - 3) . "others";
}else{
    $last = array_pop( $array );
    $string = count( $array ) ? implode( ", ", $array ) . " and " . $last : $last;
    //I used exactly your code , you have several ways to do it
    //Attention ... This code may not work when your array has only one element
}
printf( 'Authors: %s', $string );


这里有一种方法:

首先内爆前两个数组元素。(
array\u-splice
将从
$array
中删除它们)

如果有,就加上第三个。(
array\u shift
也会将其从
$array
中删除。)

数一数其余的,如果还有剩余的话,把它们加起来

if ($n = count($array)) {
    $string .= " and $n other". ($n > 1 ? 's' : '');
    //                           conditional pluralization
}

我建议您将问题视为一个可以进行单元测试的函数,这样就更容易构建,并且可以灵活地调整解决方案

  • 使用计数器跟踪数组:总计、第一个区块、尾部
  • 考虑任何特殊情况,如2个元素、3个元素等
  • 使用
    array\u pop
  • 使用
    array\u slice
    从“take N”参数中获取第一个块
  • 根据需要进行内爆和浓缩,以获得所需的结果
这里有一个例子说明了它是如何实现的

<?php

function niceAuthorsPrint(array $authors, $takeCount){

  $totalAuthors = count( $authors );
  $tailCount = $totalAuthors - $takeCount;

  $first = array_slice($authors, 0, $takeCount);

  $othersLabel = $tailCount == 1 ? 'other' : 'others';

  $string = implode( ", ", $first );

  if($tailCount > 0){
     $string .= " and " . $tailCount . ' ' . $othersLabel;      
  }

  return $string;
}

// take 3
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 3) ."\n";

// take 4
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 4) ."\n";

// take 5
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 5) ."\n";

?>


可供替代的 处理特殊情况和打印最后一项值的替代方法

<?php

// array of authors, number of authors to take first
function niceAuthorsPrint(array $authors, $takeCount){

  $totalAuthors = count( $authors );

  if($totalAuthors >= 3 && $takeCount >= $totalAuthors)
  {
      $takeCount = 2;
  }

  if($totalAuthors == 2 && $takeCount >= $totalAuthors)
  {
      $takeCount = 1;
  }

  $last = array_pop($authors);

  $tailCount = $totalAuthors - $takeCount;

  $first = array_slice($authors, 0, $takeCount);

  $othersLabel = $tailCount == 1 ? $last : $tailCount . ' others';

  $string = implode( ", ", $first );

  if($tailCount > 0){
     $string .= " and " . $othersLabel;      
  }

  return $string;
}

// take 3
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 3) ."\n";

// take 4
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 4) ."\n";

// take 5
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 5) ."\n";

// take original count, take first three as default
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 5) ."\n";

// take original count, take first three as default
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3'), 3) ."\n";

// take original count, take first three as default
echo niceAuthorsPrint(array( 'user1', 'user2'), 2) ."\n";
随时调整以适应您的需要


虽然我一直在寻找处理此类任务的巧妙计算过程,但我会提供一个更为简单的替代方案。(作为记录,我讨厌编写一组
if else
条件,就像我讨厌
switch case
块的冗长一样。)对于这个狭隘的任务,我选择放弃我喜欢的数组函数,有条件地用它们的自定义分隔符打印值

虽然此解决方案在页面上的可伸缩性可能最低,但它可能是最容易理解的(将代码与其生成的输出关联起来)。如果这是关于“喜欢”之类的,我并不认为对可伸缩性的需求很大——但我可能错了

密切关注2计数案例。我的解决方案用
来分隔元素,而不是
,这似乎更适合英语/人类。

代码:()

输出:

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、 在美国,相同处理方式的更紧凑版本:

代码:()



在一次代码审查中,我意识到了一种“pop prepend push”技术:

您的目标仅仅是打印它吗?或者你想制作新的阵列?@M4HdYaR首先非常感谢你试图帮助我。第二,使用我在代码中使用的相同数组。我想打印第一个和第二个用户,然后在第三个用户之前添加单词“and”,如果数组中有更多用户,则添加x个其他用户。相关:@mickmackusa这里是数组$array=array('user1','user2','user3','user4','user5');我想从这个数组中打印以下内容:作者:user1、user2、user3和其他2。(我想打印第一个和第二个用户,并用逗号分隔,然后在第三个用户之前添加“and”,如果数组中有更多用户,则添加x个其他用户。)。你现在明白了吗?你会一直有至少3个元素吗?我们是否需要考虑
0
元素、
1
元素、
2
元素的可能性?我认为您的代码有问题,它是打印给我的:作者:-3其他人。您可以添加完整的代码吗?可以在第一个响应中使用:
$string=$array[0]。'和'$数组[1]。'和'$数组[2]。'和'。($count-3)。'其他人的
变量
$others
不需要。只是
()
缺少到
$count-3
M4HdYaR谢谢你,我知道你在回答中想做什么,这有助于我进一步改进你的答案@JérômeTeisseire谢谢你,在你发表评论之前我已经这样做了:)。这个答案没有产生所需的输出。我去掉了一个逗号,但看起来不错。加上1…如果我能伪造一个有价值的解决方案,我仍然可以发布一个替代解决方案。@Don’我为2元素案例提供了一个未提及的便利。看看我的新帖子。你为什么要编辑你的答案?在任何编辑之前,你的回答绝对符合我的要求。只有一点你忘记了,第三个用户用一个单词“and”来分隔它,而不是像这样的逗号:user1、user2、user3和其他2。只有当数组的计数大于3且等于3时,才会发生这种情况,结果将是:user1、user2和user3。您可以查看答案的编辑历史记录。这里是前面代码的链接:我已经为我的项目编写了我想要的代码,我只是告诉你如何完成你的答案,将其标记为最佳答案。正如我在评论中告诉你的那样,如果你需要的话,你的旧代码可以毫无问题地工作。无论如何,非常感谢你的努力:)很好的回答米克:)我一直在想不同的方法来做。。。我应该停止这样做,开始我的实际工作。(该死的上瘾)
user1, user2, user3 and 2 others
user1, user2, user3, user4 and 1 other
user1, user2, user3, user4, user5
<?php

// array of authors, number of authors to take first
function niceAuthorsPrint(array $authors, $takeCount){

  $totalAuthors = count( $authors );

  if($totalAuthors >= 3 && $takeCount >= $totalAuthors)
  {
      $takeCount = 2;
  }

  if($totalAuthors == 2 && $takeCount >= $totalAuthors)
  {
      $takeCount = 1;
  }

  $last = array_pop($authors);

  $tailCount = $totalAuthors - $takeCount;

  $first = array_slice($authors, 0, $takeCount);

  $othersLabel = $tailCount == 1 ? $last : $tailCount . ' others';

  $string = implode( ", ", $first );

  if($tailCount > 0){
     $string .= " and " . $othersLabel;      
  }

  return $string;
}

// take 3
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 3) ."\n";

// take 4
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 4) ."\n";

// take 5
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 5) ."\n";

// take original count, take first three as default
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 5) ."\n";

// take original count, take first three as default
echo niceAuthorsPrint(array( 'user1', 'user2', 'user3'), 3) ."\n";

// take original count, take first three as default
echo niceAuthorsPrint(array( 'user1', 'user2'), 2) ."\n";
user1, user2, user3 and 2 others
user1, user2, user3, user4 and user5
user1, user2 and 3 others
user1, user2 and 3 others
user1, user2 and user3
user1 and user2
$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
---
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' : '');
    }
}
$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);
}