Count() 返回 1,并用逗号分隔字符串并爆炸

发布于 2024-12-03 05:46:19 字数 411 浏览 2 评论 0原文

我有一个存储以逗号分隔的标签的字段。我正在尝试计算列出的项目数量。

假设我已经从数据库中提取了数据,并且 $tags 变量具有以下信息:

$tags = "Videos,Magazines,Store";

// First separate tags by commas, put into into array
$tagArray = explode(",",$tags);

// Count how many items are in the array
$arrayCount = count($tagArray);

无论数组中是否有项目,它总是返回“1”。 $tags 变量可以有任意数量的项目 - 从空到单个项目(如“视频”)到多个项目“视频、游戏、商店”等。

有人可以帮我解决我做错的事情吗?

I have a field that stores tags comma seperated. I'm trying to count the number of items listed.

Let's say I've already pulled the data from the DB, and the $tags variable has the following info:

$tags = "Videos,Magazines,Store";

// First separate tags by commas, put into into array
$tagArray = explode(",",$tags);

// Count how many items are in the array
$arrayCount = count($tagArray);

That always returns "1", regardless if there's an item in the array or not. the $tags variable can have any number of items - from empty, to a single item like "Videos" to multiple items "Videos,Games,Store" etc.

Can someone help me out on what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫烠⑼条掵仅有一顆心 2024-12-10 05:46:19

来自PHP手册:

如果分隔符包含字符串中不包含的值并且使用负限制,则将返回一个空数组,否则将返回包含字符串的数组。

所以,简单来说 - 如果在字符串中找不到分隔符,则爆炸不执行任何操作。如果您的变量包含空字符串,count() 将返回 1。您需要 NULL 值才能让 count() 返回 0。试试

这个:

    $tags = "Videos,Magazines,Store";

    // First separate tags by commas, put into into array
$tagArray = ($tags != '')?explode(",",$tags):NULL; // Count how many items are in the array
$arrayCount = count($tagArray);

From PHP manual:

If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.

So, simply - if delimiter is not found in string, explode do nothing. If Your variable contains empty string, count() will return 1. You need NULL value for count() to return 0.

Try this:

    $tags = "Videos,Magazines,Store";

    // First separate tags by commas, put into into array
$tagArray = ($tags != '')?explode(",",$tags):NULL; // Count how many items are in the array
$arrayCount = count($tagArray);
生死何惧 2024-12-10 05:46:19

您的代码工作正常,它按预期返回 3,因为您提供了正确的输入字符串来运行代码,但是当您将数据库字段的值分配给 时出现问题$tags,因为它也可以包含空字符串,正如您在问题中所说。

正如您所说,数据库字段中可以有零个或多个零个标签,因此当 $tags 不包含标签或空字符串时,则为 php explode() 函数的手册说:

如果分隔符包含字符串中未包含的值,则将返回包含字符串(如 [ " " ])的数组

因此,当您的 $tags 包含空字符串时,explode() 返回包含空字符串的数组,所以现在您的 < code>$tagArray=[""],爆炸后您正在使用 count() 函数,因此按照 count()的php手册

返回 array_or_countable 中的元素数量。 当参数既不是数组也不是实现了Countable接口的对象时,返回1。有一个例外,如果 array_or_countable 为 NULL,将返回 0

因为您的 $tagArray 不是 NULL,而是 $tagArray=[""],所以 count($tagArray) 返回一个.

因此,为了解决这个问题,请使用下面的代码:

$tags = "Videos,Magazines,Store";
// it can also contains empty string like $tags = ""
$arrayCount = ($tags)?count(explode(",",$tags)):0;
//here $arrayCount will have 3 as expected, But if your $tags contains empty string it will return 0 instead of 1.

Your code works fine, it returns 3 as expected, because you have provided right input string to work the code, but problem arise when you assign database field's value to $tags, because it can also contains empty string as you have said in your question.

so as you have told it can be zero or more than zero tags in db field, so when $tags contains no tags or empty string then as php explode() function's manual says:

If delimiter contains a value that is not contained in string then array containing string like [ " " ] will be returned.

So when your $tags contain empty string then explode() returns array containing empty string, so now your $tagArray=[""], after exploding you are using count() function, so as per php manual of count() It

Returns the number of elements in array_or_countable. When the parameter is neither an array nor an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned.

so because your $tagArray is not NULL but $tagArray=[""], so count($tagArray) is returning one.

So for solving it use the code below:

$tags = "Videos,Magazines,Store";
// it can also contains empty string like $tags = ""
$arrayCount = ($tags)?count(explode(",",$tags)):0;
//here $arrayCount will have 3 as expected, But if your $tags contains empty string it will return 0 instead of 1.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文