有時候需要判斷array的維度,上網查了下很多事類似 http://www.poluoluo.com/jzxy/201306/215088.html 這種寫法,拉雜又搞不懂啥意思。下面是我寫的:
<code><span>private</span><span>static</span><span><span>function</span><span>array_depth</span><span>(<span>$array</span>)</span> {</span><span>if</span>(!is_array(<span>$array</span>)) <span>return</span><span>0</span>; <span>$max_depth</span> = <span>1</span>; <span>foreach</span> (<span>$array</span><span>as</span><span>$value</span>) { <span>if</span> (is_array(<span>$value</span>)) { <span>$depth</span> = array_depth(<span>$value</span>) + <span>1</span>; <span>if</span> (<span>$depth</span> > <span>$max_depth</span>) { <span>$max_depth</span> = <span>$depth</span>; } } } <span>return</span><span>$max_depth</span>; }</code>
以上就介紹了PHP判斷數組Array的維度已封裝函數,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。