如何在php中通过指定开始搜索的索引来搜索数组
如何在php中通过指定开始搜索的索引来搜索数组 例如: 假定 是否有任何函数可以实现上述伪代码 这样,如何在php中通过指定开始搜索的索引来搜索数组,php,arrays,search,indexing,Php,Arrays,Search,Indexing,如何在php中通过指定开始搜索的索引来搜索数组 例如: 假定 是否有任何函数可以实现上述伪代码 这样,$key将返回索引:8 我的意思是,我想从索引6开始搜索,以便它返回索引8=>的值为5,因为值5是我在数组中搜索的目标。我想你可以使用数组\u切片 $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 (
$key
将返回索引:8
我的意思是,我想从索引6开始搜索,以便它返回索引8=>的值为5,因为值5是我在数组中搜索的目标。我想你可以使用
数组\u切片
$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));
您能否为示例数组提供您想要开始的密钥,因为这样可以节省试图回答您问题的人员的时间。虽然,也许会有帮助。提供的价值观谢谢但不满意为什么@myckhel,那么您的期望是什么。你能澄清一下吗?我希望你现在能理解
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));