php array_walk调试示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php array_walk调试示例相关的知识,希望对你有一定的参考价值。
<span style="color: #000000; font-weight: bold;">function</span> debug_val<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$val</span>, <span style="color: #0000ff;">$key</span>=<span style="color: #ff0000;">''</span>, <span style="color: #0000ff;">$depth</span>=<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$val</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// call this function again with the "sub-array":</span>
<a href="http://www.php.net/array_walk"><span style="color: #000066;">array_walk</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$val</span>, <span style="color: #ff0000;">'debug_val'</span>, <span style="color: #0000ff;">$depth</span><span style="color: #cc66cc;">+5</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
<span style="color: #808080; font-style: italic;">// if we hit a string or bool, etc. then print it:</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <a href="http://www.php.net/str_repeat"><span style="color: #000066;">str_repeat</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'&nbsp;'</span>, <span style="color: #0000ff;">$depth</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'<span style="color: blue;">'</span> . <span style="color: #0000ff;">$key</span> . <span style="color: #ff0000;">'</span>: '</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <a href="http://www.php.net/var_export"><span style="color: #000066;">var_export</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$val</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"<br/><span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">/*example-start*/</span>
<span style="color: #808080; font-style: italic;">// setup the test array </span>
<span style="color: #0000ff;">$array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
<span style="color: #ff0000;">'php'</span>,
<span style="color: #ff0000;">'cool'</span>,
<a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'foo'</span>, <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'mixed'</span> => <span style="color: #ff0000;">'bar'</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>,
<span style="color: #ff0000;">'php'</span> => <span style="color: #ff0000;">'array'</span>,
<span style="color: #ff0000;">'yes'</span> => <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #ff0000;">'no'</span> => <span style="color: #000000; font-weight: bold;">false</span>
<span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// debug the array</span>
debug_val<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$array</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">/*example-end*/</span>
以上是关于php array_walk调试示例的主要内容,如果未能解决你的问题,请参考以下文章
PHP array_walk() 函数,对数组中应用自定义函数
php [php:array_walk]带闭包的破坏性阵列扫描。 #PHP