この投稿は xuzuning によって最終編集されました: 2015-03-19 15:27:56
$fruit = "banana"; <br />$fruits = array("apple","banana","orange"); <br />if( in_array($fruit,$fruits) ) {<br /> $fruits[array_search($fruit, $fruits)] = "pear";<br />}<br />print_r($fruits);<br />
Array<br />(<br /> [0] => apple<br /> [1] => pear<br /> [2] => orange<br />)<br /><br />
この種の要件の場合、通常、最初に in_array をチェックする必要はありません
$fruit = "banana"; <br />$fruits = array("apple","banana","orange"); <br />if(false !== ($t = array_search($fruit, $fruits)) ) {<br /> $fruits[$t] = "pear";<br />}<br />print_r($fruits);<br />