如何通过指定从php开始搜索的索引来搜索数组

时间:2018-04-30 10:30:19

标签: php arrays search indexing

如何通过指定从php

开始搜索的索引来搜索数组

例如:     假设

mapDispatchToProps

是否有任何可以实现上述伪代码的函数 这样$needle_to_search = 5; $array_to_search = [6,8,4,9,7,5,9,4,5,9,3,5,4,6,7]; $index_to_start_search_from = 6; $key = array_search($needle_to_search, $array_to_search, $index_to_start_search_from); 将返回索引:8

我的意思是我想从索引6开始搜索,以便返回索引8 =>其值为5,因为值5是我在数组中搜索的目的。

1 个答案:

答案 0 :(得分:0)

我认为你可以使用array_slice

$needle_to_search = 5;
$array_to_search = [6,8,4,9,7,5,9,4,5,9,3,5,4,6,7];
$index_to_start_search_from = 6;

//输出

Array
(
    [0] => 6
    [1] => 8
    [2] => 4
    [3] => 9
    [4] => 7
    [5] => 5
    [6] => 9
    [7] => 4
    [8] => 5
    [9] => 9
    [10] => 3
    [11] => 5
    [12] => 4
    [13] => 6
    [14] => 7
)

// return 5
echo array_search($needle_to_search, $array_to_search);

// return 8
echo array_search($needle_to_search, array_slice($array_to_search, $index_to_start_search_from, null, true));