我有一个数组列表(对于这个例子,我使用谷歌颜色)。我希望能够搜索多个键/值对,如果找到该对数组然后返回true,否则返回false。
输入如下面的颜色和lenscolor对: 此值来自动态下拉列表 排列 ( [颜色] =>东京龟,哑光黄金&绿色 [LensColor] =>绿色 )
在此数组中搜索颜色和lenscolor对 此值是动态来自xml文件(在数组中转换) 排列 ( [0] =>排列 ( [颜色] =>羽毛卡梅尔,哑光青铜色&棕色 [LensColor] =>棕色 )
[1] => Array
(
[Color] => Matte Black,matte Black & Smoke
[LensColor] => Smoke
)
[2] => Array
(
[Color] => Tokyo Tortoise,matte Gold & Green
[LensColor] => Green
)
[3] => Array
(
[Color] => Matte Black,matte Black & Smoke
[LensColor] => Green
)
[4] => Array
(
[Color] => Feathered Carmel,matte Gunmetal & Brown
[LensColor] => Green
)
[5] => Array
(
[Color] => Feathered Carmel,matte Gunmetal & Brown
[LensColor] => Smoke
)
)
答案 0 :(得分:0)
这是非常简单的想法:
$arr =Array(Array
(
"Color" => "Matte Black,matte Black & Smoke",
"LensColor" => "Smoke"
),
Array(
"Color" => "Tokyo Tortoise,matte Gold & Green",
"LensColor" => "Green"
),
Array(
"Color" => "Matte Black,matte Black & Smoke",
"LensColor" => "Green"
),
Array(
"Color" => "Feathered Carmel,matte Gunmetal & Brown",
"LensColor" => "Green"
),
Array(
"Color" => "Feathered Carmel,matte Gunmetal & Brown",
"LensColor" => "Smoke"
)
);
$founded = false;
$Color ="Tokyo Tortoise,matte Gold & Green";
$LensColor="Green";
echo $Color;
for ($i=0; $i<count($arr);$i++){
if($arr[$i]['Color']==$Color && $arr[$i]['LensColor']==$LensColor){
$founded=true;
break;
}
}
echo var_dump($founded);
它可以更加柔和。
答案 1 :(得分:0)
这是迭代搜索策略的直接实现:
var boxTemplatesIds = ko.observableArray(self.boxTemplatesList().map(
function(item) { return item.id(); }
));
输出显然是:
<?php
$haystack = [
[
'Color' => 'Matte Black,matte Black & Smoke',
'LensColor' => 'Smoke'
],
[
'Color' => 'Tokyo Tortoise,matte Gold & Green',
'LensColor' => 'Green'
],
[
'Color' => 'Matte Black,matte Black & Smoke',
'LensColor' => 'Green'
],
[
'Color' => 'Feathered Carmel,matte Gunmetal & Brown',
'LensColor' => 'Green'
],
[
'Color' => 'Feathered Carmel,matte Gunmetal & Brown',
'LensColor' => 'Smoke'
]
];
$needle = [
'Color' => 'Tokyo Tortoise,matte Gold & Green',
'LensColor' => 'Green'
];
$result = false;
array_walk(
$haystack,
function ($entry) use ($needle, &$result) {
if ( ($entry['Color'] === $needle['Color'])
&& ($entry['LensColor'] == $needle['LensColor']) ) {
$result = true;
}
}
);
var_dump($result);
另一种策略是过滤干草堆:
bool(true)
当然的输出是:
<?php
$haystack = [
[
'Color' => 'Matte Black,matte Black & Smoke',
'LensColor' => 'Smoke'
],
[
'Color' => 'Tokyo Tortoise,matte Gold & Green',
'LensColor' => 'Green'
],
[
'Color' => 'Matte Black,matte Black & Smoke',
'LensColor' => 'Green'
],
[
'Color' => 'Feathered Carmel,matte Gunmetal & Brown',
'LensColor' => 'Green'
],
[
'Color' => 'Feathered Carmel,matte Gunmetal & Brown',
'LensColor' => 'Smoke'
]
];
$needle = [
'Color' => 'Tokyo Tortoise,matte Gold & Green',
'LensColor' => 'Green'
];
$result = array_filter(
$haystack,
function ($entry) use ($needle) {
return ( ($entry['Color'] === $needle['Color'])
&& ($entry['LensColor'] == $needle['LensColor']));
}
);
var_dump($result);
更新: 在对问题的评论中,您现在提出一个更复杂的问题,即针和干草堆数组元素都是动态性的,因此不可能基于固定数组条目进行匹配。虽然没有确定完全匹配的标准,但我可以简单地暗示哪些修改可能指向正确的方向:
必须更改匹配条件,最简单的方法是测试两个数组的标识。所以你在迭代循环中使用类似下面的东西作为比较回调而不是上面的代码:
array(1) {
[1]=>
array(2) {
["Color"]=>
string(33) "Tokyo Tortoise,matte Gold & Green"
["LensColor"]=>
string(5) "Green"
}
}
如果两个数组的键组和包含的值都相同,则只接受匹配。