Home > Article > Backend Development > PHP中使用foreach统制数组的扫描
PHP中使用foreach控制数组的扫描
<br />foreach ($c_f_arr as &$c_f) { <br /> if (trim($c_f[0])==trim($char)) { <br /> echo "$char is exist <br>"; <br /> $c_f[1]++; <br /> <br /> echo "hahaha <br>"; <br /> echo "this is cf: $c_f[1] <br>"; <br /> } <br /> else { <br /> echo "$char is not exist <br>"; <br /> $c_new=array(trim($char),'1'); <br /> array_push($c_f_arr, $c_new); <br /> $n++; <br /> } <br /> } <br />
<br /><?php<br />$arr = array(array('a',0),array('b',0),array('c',0));<br />$char = 'a';<br />$is_exists = 0;<br />for($i=0,$len=count($arr); $i<$len; $i++){<br /> if($arr[$i][0] == $char){<br /> $arr[$i][1]++;<br /> $is_exists = 1;<br /> break;<br /> }<br />}<br /><br />if($is_exists==0){<br /> array_push($arr, array($char,0));<br />}<br /><br />print_r($arr);<br />?><br />