簡體   English   中英

數組以連續順序搜索多個值

[英]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 [];
}

測試1

$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)
}

測試2

$s = [
    "i",
    "like",
    "cats",
];

return array_search_all_consec($s, $o);

// Result
array(3) {
  [0] =>
  int(1)
  [1] =>
  int(2)
  [2] =>
  int(3)
}

測試3

$s = [
    "i",
    "lov3",
    "cats",
];

return array_search_all_consec($s, $o);

// Result
array(0) {
}

測試4

$s = [
    "cats",
    "i",
    "love",
];

return array_search_all_consec($s, $o);

// Result
array(0) {
}

測試5

$s = [
    "i",
    "love",
    "cats",
    "blah",
];

return array_search_all_consec($s, $o);

// Result
array(0) {
}


所以你有它。 如果有人可以提高功能的效率,請您貢獻力量!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM