PHP 中的字符串真的是字符数组还是其他什么?

发布于 2024-12-03 15:48:12 字数 271 浏览 2 评论 0原文

考虑

$foo = "abcdefg";

echo $foo[0]; //outputs a

一下,看起来字符串就像字符数组,但为什么

foreach($foo as $char)
{
echo $char;
}

不起作用并给出以下警告?

Warning: Invalid argument supplied for foreach() 

Consider

$foo = "abcdefg";

echo $foo[0]; //outputs a

So it seems like strings are like array of characters but then why

foreach($foo as $char)
{
echo $char;
}

does not work and gives following Warning ??

Warning: Invalid argument supplied for foreach() 

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

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

发布评论

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

评论(4

不爱素颜 2024-12-10 15:48:12

讨论了向 foreach 添加字符串迭代支持,但是拒绝了。做出这个决定主要有两个原因:

  • 它使应用程序更难调试。通常您不想迭代字符串的字符。你很少需要它。因此,如果您确实迭代了一个字符串,您可能只是犯了一个编程错误——PHP 会告诉您这一点。如果引入字符串迭代,这种错误将很难捕获。
  • 什么是“性格”? PHP 应该迭代每个字节吗?它应该迭代字符(可以是多个字节)吗?如果是这样,如果遇到格式错误的多字节序列该怎么办?它从哪里获取字符集?

为了解决这两个问题,有人建议引入一个 TextIterator,您可以向其中传递一个字符串和一个字符集。这样您就不会意外地迭代字符串,并且不存在字节与字符问题。我不确定 TextIterator 当前的状态是什么。

Adding string iteration support to foreach was discussed but declined. There were mainly two reasons for this decision:

  • It makes applications harder to debug. Usually you don't want to iterate over the characters of a string. You need that only very rarely. So if you do iterate over a string you probably just made a programming mistake - and PHP will tell you so. If string iteration were introduces this kind of error would be hard to catch.
  • What is a "character"? Should PHP iterate over each single byte? Should it iterate over characters (which can be multiple bytes)? If so, what should it do if it encounters a malformed multibyte sequence? And where does it get the charset from?

To solve both problems there was a proposal to introduce a TextIterator, which you pass a string and a charset. That way you can't accidentally iterate a string and the byte vs character problem doesn't exist. I'm not sure though what the state of the TextIterator is currently.

尐偏执 2024-12-10 15:48:12

NikiC 的回答涵盖了为什么直接这样做是不可能的。

如果您想像数组一样迭代字符串,可以使用 str_split

foreach(str_split($foo) as $char) 
{ 
    echo $char; 
} 

警告: str_split 不支持编码,因此您最终将迭代 字节且未超过字符。迭代字符有点复杂,因为没有等效的多字节分割函数。您可以使用启用正则表达式的 mb_split,查看来自 PHP.net 的评论< /a> 寻求想法。

这里还有其他答案建议您应该将字符串转换为数组,但我不明白为什么这会起作用。 文档非常明确:

对于任何类型:整数、浮点数、字符串、布尔值和资源,
将值转换为数组会产生一个带有单个值的数组
索引为零的元素和标量的值
转换。换句话说, (array)$scalarValue 与
数组($scalarValue)。

事实上,这样做并不像建议的那样工作

NikiC's answer covers why doing this directly is not possible.

If you want to iterate over a string as if it were an array, you can be explicit by using str_split:

foreach(str_split($foo) as $char) 
{ 
    echo $char; 
} 

Warning: str_split is not encoding-aware, so you will end up iterating over bytes and not over characters. Iterating over characters is a little more involved, as there is no equivalent multibyte split function. You can roll your own using the regex-enabled mb_split, look at the comments from PHP.net for ideas.

There are other answers here suggesting you should cast the string to an array, but I don't understand why that would work. The documentation is pretty explicit:

For any of the types: integer, float, string, boolean and resource,
converting a value to an array results in an array with a single
element with index zero and the value of the scalar which was
converted. In other words, (array)$scalarValue is exactly the same as
array($scalarValue).

And indeed, doing this does not work as suggested.

猫烠⑼条掵仅有一顆心 2024-12-10 15:48:12

尽管可以使用方括号来寻址它们的字符,字符串不是数组< /a>.强调我的:

可以通过使用方数组括号指定字符串后所需字符的从零开始的偏移量来访问和修改字符串中的字符,如 $str[42] 中。为此,可以将字符串视为字符数组。

Even though their characters can be addressed using square brackets, strings aren't arrays. Emphasis mine:

Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.

白日梦 2024-12-10 15:48:12

它被称为“语法糖”。

例如,在 5.4 中,您将能够执行以下操作 echo func()[0];
并不意味着函数实际上是一个字符数组。
这只是一个语法。

it is called "syntax sugar".

For example, in 5.4 you'll be able to do like this echo func()[0];
That doesn't mean that a function is really an array of charactes.
It's just a syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文