在一维数组中使用array_search很简单
$array = array("apple", "banana", "cherry"); $searchValue = "cherry"; $key = array_search($searchValue, $array); echo $key;
但是多维数组呢?
#RaceRecord [CarID] [ColorID] [Position] [0] 1 1 3 [1] 2 1 1 [2] 3 2 4 [3] 4 2 2 [4] 5 3 5
例如,我想获取位置为1的汽车的索引。我该怎么做?
function find_car_with_position($cars, $position) { foreach($cars as $index => $car) { if($car['Position'] == $position) return $index; } return FALSE; }