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教程有兴趣的朋友有所帮助。