我将这个单词数组存储在变量$tags
中。
print_r($tags);
Array
(
[0] => Array
(
[token] => Facebooks
[tag] => NNS
)
[1] => Array
(
[token] => new
[tag] => JJ
)
[2] => Array
(
[token] => Slingshot
[tag] => NN
)
[3] => Array
(
[token] => messaging
[tag] => VBG
)
[4] => Array
(
[token] => app
[tag] => NNP
)
[5] => Array
(
[token] => the
[tag] => DT
)
[6] => Array
(
[token] => one
[tag] => NN
)
[7] => Array
(
[token] => that
[tag] => IN
)
[8] => Array
(
[token] => forces
[tag] => NNS
)
[9] => Array
(
[token] => friendship
[tag] => NN
)
[10] => Array
(
[token] => through
[tag] => IN
)
[11] => Array
(
[token] => a
[tag] => DT
)
[12] => Array
(
[token] => special
[tag] => JJ
)
[13] => Array
(
[token] => reciprocation
[tag] => NN
)
[14] => Array
(
[token] => mechanic
[tag] => NN
)
[15] => Array
(
[token] => has
[tag] => VBZ
)
[16] => Array
(
[token] => today
[tag] => NN
)
[17] => Array
(
[token] => launched
[tag] => VBN
)
[18] => Array
(
[token] => worldwide.
[tag] => JJ
)
)
我试图仅提取包含[tag] => NN
的数组,并使用此删除其他数组:
$kali = array("NN");
echo '<br>';
echo '<br>';
foreach ( $tags as $k => $array ) {
if ( $array['tag'] != $kali[0] )
{
unset($tags[$k]);
}
}
print_r($tags);
这是输出:
Array
(
[6] => Array
(
[token] => one
[tag] => NN
)
[13] => Array
(
[token] => reciprocation
[tag] => NN
)
[16] => Array
(
[token] => today
[tag] => NN
)
)
任务是我做错了什么?为什么dost显示所有数组但只有3个?
总共这个例子中的是6,它显示在3.为什么?
答案 0 :(得分:1)
有时会将spaces
或\n
或\r
等特殊字符添加到字符串的末尾,因此将字符串与另一个字符串进行比较也会评估特殊字符。为了跳过它们,您需要trim
字符串然后比较它。
示例强>
if ( trim($array['tag']) != trim($kali[0]) )
{
// code goes here
}