[英]Array search multiple values in consecutive order
取給定的數組:
// Original
array:14 [
0 => "hello"
1 => "i"
2 => "like"
3 => "cats"
4 => "they're"
5 => "cute"
6 => "and"
7 => "cuddly"
8 => "you"
9 => "know"
10 => "well"
11 => "i"
12 => "love"
13 => "cats"
]
// Sliced
array:6 [
0 => "like"
1 => "cats"
2 => "they're"
3 => "cute"
4 => "and"
5 => "cuddly"
]
我想檢查original
數組的sliced
值並檢索其原始鍵。 僅當連續值匹配時,才必須發生這種情況。
最終,類似:
return array_keys_from_consec_values($original, $sliced);
// Result
array:14 [
0 => 2
1 => 3
2 => 4
3 => 5
4 => 6
5 => 7
]
sliced
不是在original
數組上使用array_slice()
的結果。 否則,我將使用該功能並利用preserve keys參數。
想要避免歧義。 例如,對cats
的簡單array_search
將返回鍵3
,但13
也存在。 這可能會使功能失效。
如果original
(順序)中不存在任何sliced
值,則應返回一個空數組。 可能會失敗的sliced
數組的示例:
// Sliced
array:6 [
0 => "like"
1 => "cats"
2 => "they're"
3 => "cut3"
4 => "and"
5 => "cuddly"
]
任何建議,請牢記速度...
據我了解,您想基於$slice
值從$original
數組中獲取索引,可以使用array_flip()
和array_unique()
函數:
function array_keys_from_consec_values($original, $slice){
// Get rid of duplicates and flip the keys with the values
$index = array_unique($original);
$index = array_flip($index);
$result = [];
// Loop through the slice
foreach($slice as $key => $value){
// Check if the value exists in the indexed array
if(!isset($index[$value])){
// Return an empty array
return array();
}
// And get the key from the flipped array
$result[$key] = $index[$value];
}
return $result;
}
var_dump(array_keys_from_consec_values($original, $slice));
這將給出:
array (size=6)
0 => int 2
1 => int 3
2 => int 4
3 => int 5
4 => int 6
5 => int 7
這適用於您的示例數據,可能已經足夠了:
$r = array_keys(array_unique(array_intersect($o, $s)));
如果沒有,則因為有一個切片:
if(($k = array_search($s[0], $o)) === false ||
array_values($r = array_slice($o, $k, count($s), true)) != $s) {
$r = [];
}
以您的示例為例:
Array
(
[2] => like
[3] => cats
[4] => they're
[5] => cute
[6] => and
[7] => cuddly
)
我想知道您想要獲得鑰匙的事實:
$r = array_values(array_flip($r));
如果切片是原始的實際array_slice
,則將true
作為第四個參數傳遞以保留鍵。
努力工作的人,我承認這個要求很難解釋-因此並沒有得到我真正想要的。 我意識到,我想要的實際上是一個array_search
,當連續找到針時,它會返回多個鍵。 我繼續自己做這個功能:
/**
* Search for consecutive needles in haystack and return the corresponding keys.
*
* @param array $needles => Array values.
* @param array $haystack => Array to search.
* @param int $searchDirection => [0] (forwards) | [1] (backwards).
* @return array
*/
function array_search_all_consec(array $needles, array $haystack, $searchDirection = 0)
{
$needlesInScope = array_values($needles); // Keys not relevant, reset them.
$haystackInScope = $haystack;
if (in_array(reset($needlesInScope), $keys = array_keys($haystackInScope))) {
$needlesLength = count($needlesInScope);
foreach (($searchDirection == 0 ? $keys : array_reverse($keys)) as $offset) {
$length = $offset + $needlesLength;
$sliced = array_slice($haystackInScope, $offset, $length);
if (empty(array_diff_assoc($needlesInScope, $sliced))) {
return range($offset, $length - 1);
}
}
}
return [];
}
$o = [
0 => "hello",
1 => "i",
2 => "like",
3 => "cats",
4 => "they",
5 => "cute",
6 => "and",
7 => "cuddly",
8 => "you",
9 => "know",
10 => "well",
11 => "i",
12 => "love",
13 => "cats",
];
$s = [
"i",
"love",
"cats",
];
return array_search_all_consec($s, $o);
// Result
array(3) {
[0] =>
int(11)
[1] =>
int(12)
[2] =>
int(13)
}
$s = [
"i",
"like",
"cats",
];
return array_search_all_consec($s, $o);
// Result
array(3) {
[0] =>
int(1)
[1] =>
int(2)
[2] =>
int(3)
}
$s = [
"i",
"lov3",
"cats",
];
return array_search_all_consec($s, $o);
// Result
array(0) {
}
$s = [
"cats",
"i",
"love",
];
return array_search_all_consec($s, $o);
// Result
array(0) {
}
$s = [
"i",
"love",
"cats",
"blah",
];
return array_search_all_consec($s, $o);
// Result
array(0) {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.