我有一个
数组 CollectionX<String> col = CollectionX.fromCollection(orgCol);
col.zipWithIndex()
.toMap(k->k.v1, v->v.v2);
我有一个字符串
ReactiveSeq.fromCollection(col)
.zipWithIndex()
.toMap(k->k.v1, v->v.v2);
我试过
$device_name = ["Xperia Z Ultra", "Xbox One", "Withing", "Win 7", "Surface Pro 4", "Surface Book", "Surface 3", "Play Station 4", "One Plus 2", "Nokia Lumia", "No Img", "Nexus 6p", "Nest", "Moto X", "Mac Pro", "Mac Book", "Mac Book Air", "Lg G4", "Laptop", "Iphone 6", "Iphone 6 S", "Iphone 6 S Plus", "Iphone 6 Plus", "Iphone 5s", "Iphone 5", "Iphone 4", "Ipad", "Ipad Pro", "Hue", "Htc One M9", "Hp Laptop", "Galaxy S6", "Galaxy S6 Edge", "Galaxy S5", "Galaxy Note 5", "Galaxy Note 4", "Galaxy Note 3", "Galaxy Note 2", "Chromecast", "Chrome Book", "Blackberry Z30", "Blackberry Z10", "Blackberry Prev", "Blackberry Bold", "August", "Apple Watch", "Apple Tv", "Ap", "Androidtablet"]
我不确定我现在拥有的是实现理想结果的最佳方法。
我愿意接受任何建议或更好的方式。
答案 0 :(得分:5)
$devices = ["Xperia Z Ultra", "Xbox One", "Withing", "Win 7", "Surface Pro 4", "Surface Book", "Surface 3", "Play Station 4", "One Plus 2", "Nokia Lumia", "No Img", "Nexus 6p", "Nest", "Moto X", "Mac Pro", "Mac Book", "Mac Book Air", "Lg G4", "Laptop", "Iphone 6", "Iphone 6 S", "Iphone 6 S Plus", "Iphone 6 Plus", "Iphone 5s", "Iphone 5", "Iphone 4", "Ipad", "Ipad Pro", "Hue", "Htc One M9", "Hp Laptop", "Galaxy S6", "Galaxy S6 Edge", "Galaxy S5", "Galaxy Note 5", "Galaxy Note 4", "Galaxy Note 3", "Galaxy Note 2", "Chromecast", "Chrome Book", "Blackberry Z30", "Blackberry Z10", "Blackberry Prev", "Blackberry Bold", "August", "Apple Watch", "Apple Tv", "Ap", "Androidtablet"];
$input = "John's Xperia Z Ultra";
$stringContainsDevice = count(array_filter($devices, function($device) use ($input) {
return stripos($input, $device) !== false;
})) !== 0;
所以基本上你过滤数组中包含在字符串中的项目(我做了大小写不敏感,但如果你想区分大小写,只需将stripos
更改为strpos
),然后查看过滤后的数组有任何项目。
答案 1 :(得分:1)
更简单的解决方案是
$contain = 0 ;
foreach($device_name as $value){
if(strpos($input, $value)) $contain = 1;
}
如果输入包含数组的值,则contains为1,否则为0
答案 2 :(得分:1)
其他答案为$device_name array
的每次迭代进行函数调用。两者都没有提供提前退出选项(break
)。
相反,我会优化您的in_array()
流程并向您展示一个避免条件并且只会进行3次函数调用的单行程序。
$device_name = ["Xperia Z Ultra", "Xbox One", "Withing", "Win 7", "Surface Pro 4", "Surface Book", "Surface 3", "Play Station 4", "One Plus 2", "Nokia Lumia", "No Img", "Nexus 6p", "Nest", "Moto X", "Mac Pro", "Mac Book", "Mac Book Air", "Lg G4", "Laptop", "Iphone 6", "Iphone 6 S", "Iphone 6 S Plus", "Iphone 6 Plus", "Iphone 5s", "Iphone 5", "Iphone 4", "Ipad", "Ipad Pro", "Hue", "Htc One M9", "Hp Laptop", "Galaxy S6", "Galaxy S6 Edge", "Galaxy S5", "Galaxy Note 5", "Galaxy Note 4", "Galaxy Note 3", "Galaxy Note 2", "Chromecast", "Chrome Book", "Blackberry Z30", "Blackberry Z10", "Blackberry Prev", "Blackberry Bold", "August", "Apple Watch", "Apple Tv", "Ap", "Androidtablet"];
$input = "John's Xperia Z Ultra";
var_export(in_array(array_slice(explode("'s ",$input,2),-1)[0],$device_name)); // true
以下是它的工作原理:
's
(单引号,s,空格)上展开字符串,并将元素数限制为2.这意味着如果找到's
,则会有2个元素,如果不,会有1个元素。explode()
生成的最后一个元素。这个可以与end()
完成,但不在同一行 - 您需要声明一个新变量来处理这些数据。 array_slice()
的第二个参数(-1
)表示提取最后一个元素。current()
完成,但同样,您需要声明一个新变量。in_array()
来电。这是demo that runs three different input cases。
P.S。您的原始方法很好,只需要explode()
的限制。