$fruit = "banana"; $fruits = array("apple","banana","orange"); if( in_array($fruit,$fruits) ) { //符合条件 //如何把$fruits的"banana"改成"pear"?}
$fruit = "banana"; $fruits = array("apple","banana","orange"); if( in_array($fruit,$fruits) ) { $fruits[array_search($fruit, $fruits)] = "pear";}print_r($fruits);
Array( [0] => apple [1] => pear [2] => orange)この種の要件の場合、通常、最初に in_array で
$fruit = "banana"; $fruits = array("apple","banana","orange"); if(false !== ($t = array_search($fruit, $fruits)) ) { $fruits[$t] = "pear";}print_r($fruits);をチェックする必要はありません