foreach 修剪字符,我没有告诉它!

发布于 2024-11-06 17:47:26 字数 554 浏览 0 评论 0原文

我已将其清理到我知道存在问题的地方。坦率地说,我有一个 foreach 语句,将结果的一个字符全部截断。下面评论中的示例。

print_r($getarticlemultiarray);

/// this print_r returns good values like [title] => titletext [body] => bodytext
foreach ($getarticlemultiarray as $zyz)  {
    echo $zyz['title'];
// here is the problem.  This echo statement is only throwing out 1 character
 // for example with the values in the example above it's just echoing a 't'.

}  // end foreach

这个 foreach 嵌套在另一个 foreach 中,但我没有对字符串长度做任何事情,并且我没有在其他任何地方使用 $zyz 。没什么奇怪的,只是普通的单词,也没有特殊字符。

I've cleaned this up to where I know there is an issue. Bluntly I've got a foreach statement cutting off all by one character of the results. Example in comments below.

print_r($getarticlemultiarray);

/// this print_r returns good values like [title] => titletext [body] => bodytext
foreach ($getarticlemultiarray as $zyz)  {
    echo $zyz['title'];
// here is the problem.  This echo statement is only throwing out 1 character
 // for example with the values in the example above it's just echoing a 't'.

}  // end foreach

This foreach is nested inside another one, but I'm not doing anything with string lengths, and I am not using $zyz anywhere else. Nothing strange but normal words with no special characters either.

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

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

发布评论

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

评论(1

面犯桃花 2024-11-13 17:47:26

根据您所说的,看来您的 print_r 本质上是

Array (
   ['title'] => 'titletext',
   ['body'] => 'bodytext
)

在这种情况下,您的 foreach$zyz 设置为 titletext正文文本,分别。这些字符串没有“标题”键。 PHP 会将这些键视为 0,进而返回第一个字符(因此您会看到“t”)。看来你不必在这里循环。

Based on what you have said, it looks like your print_r is essentially

Array (
   ['title'] => 'titletext',
   ['body'] => 'bodytext
)

In that case, your foreach is setting $zyz to titletext and bodytext, respectively. These strings have no 'title' key. PHP will treat these key as 0 which in turn returns the first character (hence why you see a 't'). Seems like you don't have to loop here.

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