【发布时间】:2019-02-07 15:09:21
【问题描述】:
我有一系列属性,这些属性是使用 rest api 从 woocommerce 商店获取的。它看起来像:
Array
(
[0] => stdClass Object
(
[id] => 6
[name] => Modelis
[position] => 0
[visible] => 1
[variation] => 1
)
[1] => stdClass Object
(
[id] => 5
[name] => Krāsa
[position] => 1
[visible] => 1
[variation] => 1
)
)
在这个数组中,我想找到名为“Krāsa”的项目。因为它包含特殊字母“ā”,所以简单的比较不起作用:
foreach ($attributes as $item):
if (!strcmp($item->name, 'Krāsa')):
print_r('Names match');
endif;
endforeach;
尽管数组中有名称 Krāsa,但这样的 if 子句始终为假。也许这是我的背景不好,但我想知道,如何正确比较这些字符串?
非常感谢。
【问题讨论】:
-
嗯,如果我运行
!strcmp('Krāsa', 'Krāsa'),结果为真。 -
在比较字符串之前使用
htmlspecialchars怎么样? -
^
strcmp如果它们相同则返回 0 所以可以使用!
标签: php arrays string string-comparison strcmp