首頁 >後端開發 >php教程 >php數組遍歷

php數組遍歷

WBOY
WBOY原創
2016-07-29 09:06:38951瀏覽

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教程有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn