1.一維數組遍歷
<code><span>foreach</span>(<span>$array</span><span>as</span><span>$value</span>){ <span>echo</span><span>$value</span>; }</code>
2.二維數組遍歷
<code><span>foreach</span>(<span>$array</span><span>$key</span>=><span>$val</span>){ <span>echo</span><span>$key</span>.<span>'=>'</span>.<span>$val</span>; }</code>
3.多維數組遍歷
<code><span>public</span><span>static</span><span><span>function</span><span>multi_arr_foreach</span><span>(<span>$arr</span>)</span> {</span><span>static</span><span>$data</span>; <span>if</span> (!is_array (<span>$arr</span>)) { <span>return</span><span>$data</span>; } <span>foreach</span> (<span>$arr</span><span>as</span><span>$key</span> => <span>$val</span> ) { <span>if</span> (is_array (<span>$val</span>)) { <span>self</span>::multi_arr_foreach(<span>$val</span>); } <span>else</span> { <span>$data</span>[]=<span>$val</span>; } } <span>return</span><span>$data</span>; }</code>
不過還看到一個很維的寫法,還不是短維數
reee ,如果要修改value值時,要注意還要把改變的value值再賦給key<code><span><span>function</span><span>loop_array</span><span>(<span>$arr</span>)</span>{</span><span>$value</span> = is_array(<span>$arr</span>) ? array_map(<span>'loop_array'</span>,<span>$arr</span>) : <span>$arr</span>; <span>return</span><span>$value</span>; }</code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); }); 以上就介紹了php數組遍歷,包含了方面的內容,希望對PHP教程有興趣的朋友有幫助。