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;">&#40;</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;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$val</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// call this function again with the &quot;sub-array&quot;:</span>
        <a href="http://www.php.net/array_walk"><span style="color: #000066;">array_walk</span></a><span style="color: #66cc66;">&#40;</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;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</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;">&#40;</span><span style="color: #ff0000;">'&amp;nbsp;'</span>, <span style="color: #0000ff;">$depth</span><span style="color: #66cc66;">&#41;</span>;          
        <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">'&lt;span style=&quot;color: blue;&quot;&gt;'</span> . <span style="color: #0000ff;">$key</span> . <span style="color: #ff0000;">'&lt;/span&gt;: '</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;">&#40;</span><span style="color: #0000ff;">$val</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;&lt;br/&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/*example-start*/</span>
&nbsp;
<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;">&#40;</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;">&#40;</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;">&#40;</span><span style="color: #ff0000;">'mixed'</span> =&gt; <span style="color: #ff0000;">'bar'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
    <span style="color: #ff0000;">'php'</span> =&gt; <span style="color: #ff0000;">'array'</span>, 
    <span style="color: #ff0000;">'yes'</span> =&gt; <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #ff0000;">'no'</span> =&gt; <span style="color: #000000; font-weight: bold;">false</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// debug the array</span>
debug_val<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/*example-end*/</span>

以上是关于php array_walk调试示例的主要内容,如果未能解决你的问题,请参考以下文章

PHP array_walk() 函数详解

PHP array_walk() 函数

PHP array_walk() 函数,对数组中应用自定义函数

php [php:array_walk]带闭包的破坏性阵列扫描。 #PHP

PHP 合并两个二维数组 array_map 和 array_walk 的区别

php中array_walk() 和 array_map()两个函数区别