0

如果我有以下格式的数组,如何将这些数组值的一部分分配给新数组?(数值123654表示可以在这些位置分隔数组)

$fruits = array();
$vehicles = array();
$all_words = array ("123", "apple", ”orange”, ………, ”bannana”, ”123”, ”654”, ”car”, ”bus”, ………, ”train”, ”bike”, ”654”); 
//continuous ……… Indicate there can be unknown amount of array elements.
//Also, these numeric values of 123 and 645 can be changed to something convenient 

我可以看到使用$fruits = array_slice($array, 1, 5) 我可以将这个数组的一部分放入一个新数组中。但是,如果我不知道两个数字之间的数组长度(123654),我如何将这些值分配给一个新数组?

4

2 回答 2

2

您可以使用它们的索引array_search(),然后将它们用于切片,就像这样..

$index1 = array_search('123', $all_words); 
$index2 = array_search('654', $all_words); 
$vehicles = array_slice($array, $index1, $index2-$index1+1);
于 2013-07-03T05:27:35.850 回答
0

$fruits = array_slice($array, array_search('123', $array)+1, array_search('654', $array)+1)

于 2013-07-03T05:30:26.190 回答