php中數組合併的方式有兩種,具體方法容我慢慢道來:
1.array_merge()函數合併數組
具體事例如下:
<code><span><span><?php</span><span>$a</span> = <span>array</span>( <span>'where'</span> => <span>'uid=1'</span>, <span>'order'</span> => <span>'uid'</span>, <span>'limit'</span> => <span>'5'</span> ); <span>$b</span> = <span>array</span>( <span>'where'</span> => <span>'uid=2'</span>, <span>'order'</span> => <span>'uid desc'</span>, ); <span>$c</span> = array_merge(<span>$a</span>,<span>$b</span>); print_r(<span>$c</span>); <span>$d</span> = array_merge(<span>$b</span>,<span>$a</span>); print_r(<span>$d</span>);</span></span></code>
輸出結果如下:
注意where和uid字段,這兩個字段是$a和$b數組中共有的字段,在註意結果中這兩個字段值的區別,可以總結出什麼呢?Array ( [where] => uid=2 [order] => uid desc [limit] => 5 )
Array ( [where] => uid=1 [order] => uid [limit] => 5 )
<code><span><span><?php</span><span>$a</span> = <span>array</span>( <span>'where'</span> => <span>'uid=1'</span>, <span>'order'</span> => <span>'uid'</span>, <span>'limit'</span> => <span>'5'</span> ); <span>$b</span> = <span>array</span>( <span>'where'</span> => <span>'uid=2'</span>, <span>'order'</span> => <span>'uid desc'</span>, ); <span>$c</span> = <span>$a</span>+<span>$b</span>; print_r(<span>$c</span>); <span>$d</span> = <span>$b</span>+<span>$a</span>; print_r(<span>$d</span>);</span></span></code>
輸出結果如下:
如上,我們仍然關注where和order字段的值,可以得出什麼結論呢?Array ( [where] => uid=1 [order] => uid [limit] => 5 )
Array ( [where] => uid=2 [order] => uid desc [limit] => 5 )
以上就介紹了php中合併陣列的方法,包括了php,的方法方面的內容,希望對PHP教程有興趣的朋友有幫助。