[英]Separate an array with comma and string PHP
我想用逗號和單詞分隔一個數組。
這是我制作的代碼:
$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”分隔。
但這就是我想要的,但是經過 2 天多的工作后我失敗了:
作者:user1、user2、user3 和 2 個其他人。
這是一種方法:
首先內爆前兩個數組元素。 ( array_splice
將從$array
刪除它們。)
$string = implode(', ', array_splice($array, 0, 2));
如果有的話,添加第三個。 ( array_shift
也會從$array
刪除它。)
if ($array) {
$string .= ', and ' . array_shift($array);
}
計算其余的並添加它們,如果有的話。
if ($n = count($array)) {
$string .= " and $n other". ($n > 1 ? 's' : '');
// conditional pluralization
}
雖然我一直在尋找巧妙的計算過程來處理這樣的任務,但我會提供一個更簡單的替代方案。 (為了記錄,我討厭寫if-elseif-else
條件,就像我討厭switch-case
塊的冗長switch-case
。)對於這個狹窄的任務,我選擇放棄我喜歡的數組函數並有條件地打印帶有自定義分隔符的值。
雖然此解決方案在頁面上的可擴展性可能最低,但它可能是最容易理解的(將代碼與其生成的輸出相關聯)。 如果這是關於“喜歡”之類的,我真的沒有看到對可擴展性的大量需求——但我可能是錯的。
密切注意 2 計數的情況。 我的解決方案用and
而不是 分隔元素,
這似乎更適合英語/人類。
代碼:(演示)
$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
---
ps 相同處理的更緊湊版本:
代碼:(演示)
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
到最后一個元素。 我認為無論如何你旋轉這個任務,都會有一定程度的卷積。
代碼:(演示)
$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
有幾種方法可以做到,一種簡單的方法:
首先,您應該獲取數組中元素的數量,以查看它是否大於 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_pop
獲取最后一項array_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";
?>
將輸出:
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
隨意調整以適應您的需求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.