array_filter似乎不适用于带有撇号和破折号的单词 [英] array_filter doesn't seems to work for words having apostrophe and dash

查看:74
本文介绍了array_filter似乎不适用于带有撇号和破折号的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 php代码,如下所示:

$variable = \CTIME\DataPoint\get_message();  // Line A
echo '<pre>'; print_r($variable); echo '</pre>';  // Line B
echo '<pre>'; var_dump($variable); echo '</pre>';  // Line C
print_r(array_filter($variable, function ($i) { return $i->top_portion == 'Hello – World'; }));  // Line D

B行打印:

Array
(
    [0] => stdClass Object
        (
            [language] => en
            [client_id] => 12345
            [top_portion] => Hello World
        )
    [1] => stdClass Object
        (
            [language] => en
            [client_id] => 56789
            [top_portion] => Hello – World
        )

)

C行打印:

array (
  0 => 
  (object) array(
     'language' => 'en',
     'client_id' => 12345,
     'top_portion' => 'Hello World',
  ),
  1 => 
  (object) array(
     'language' => 'en',
     'client_id' => 56789,
     'top_portion' => 'Hello – World',
  ),
)

D行打印:

Array 
(
)

问题陈述:

我想知道我需要添加什么PHP代码,以便仅打印具有 [top_portion] => Hello – World

I'm wondering what PHP code I need to add so that it prints only the object which has [top_portion] => Hello – World

Array
(
    [0] => stdClass Object
        (
            [language] => en
            [client_id] => 56789
            [top_portion] => Hello – World
        )
)

这是我尝试过的:

print_r(array_filter($variable, function ($i) { return $i->top_portion == 'Hello – World'; }));

它打印;

Array
(
)

推荐答案

您的代码似乎不错.
我的猜测是这是一个字符集问题:正确的单引号"(’)是unicode字符,不属于ASCII字符集.

Your code seems good.
My guess is that this is a charset issue: the "right single quotation mark" (’) is a unicode char and is not part of ASCII charset.

如果源数据中的字符串和PHP脚本中的字符串使用不同的字符集,则它们可能是不同的(字节序列不同).

If the string from the source data and the string in your PHP script use a distinct charset, they might be different (not the same sequence of bytes).

例如,如果您使用的是UTF-8:请检查您使用get_live_today_streams()获取的数据是否为UTF-8编码,并确保您的.php文件也为UTF-8编码.

For instance, if you're using UTF-8: check that the data you're fetching with get_live_today_streams() is UTF-8 encoded, and make sure that your .php file is UTF-8 encoded as well.

(看看在本文中,了解如何使用记事本++将ANSI文件转换为UTF-8)

(Have a look at this post to see how to convert a ANSI file to UTF-8 using notepad++)

这篇关于array_filter似乎不适用于带有撇号和破折号的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆