查看完整版本: 使用递归替换数组中的内容

Kenny 2007-8-9 14:09

使用递归替换数组中的内容

             <p><P style="MARGIN: 5px; LINE-HEIGHT: 150%"><CODE><SPAN style="COLOR: #000000"><FONT face=新宋体><SPAN style="COLOR: #0000cc">&lt;</SPAN><SPAN style="COLOR: #0000cc">?</SPAN><SPAN style="COLOR: #0000ff">php</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">arr</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff0000">array</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff00ff">"&lt;小刚&gt;"</SPAN><SPAN style="COLOR: #0000cc">,</SPAN><SPAN style="COLOR: #ff00ff">"&lt;小晓&gt;"</SPAN><SPAN style="COLOR: #0000cc">,</SPAN><SPAN style="COLOR: #ff00ff">"&lt;小飞&gt;"</SPAN><SPAN style="COLOR: #0000cc">,</SPAN><SPAN style="COLOR: #ff00ff">"&lt;小李&gt;"</SPAN><SPAN style="COLOR: #0000cc">,</SPAN><SPAN style="COLOR: #ff00ff">"&lt;小红&gt;"</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">function</SPAN> arrContentReplact<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">array</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff0000">is_array</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">array</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">foreach</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">array</SPAN> <SPAN style="COLOR: #0000ff">as</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">k</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN><SPAN style="COLOR: #0000cc">&gt;</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">v</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">array</SPAN><SPAN style="COLOR: #0000cc">[</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">k</SPAN><SPAN style="COLOR: #0000cc">]</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> arrContentReplact<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">array</SPAN><SPAN style="COLOR: #0000cc">[</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">k</SPAN><SPAN style="COLOR: #0000cc">]</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">}</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">}</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">{</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">array</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> <SPAN style="COLOR: #ff0000">str_replace</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff0000">array</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff00ff">'&lt;'</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff00ff">'&gt;'</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff0000">array</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #ff00ff">'{'</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #ff00ff">'}'</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">,</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">array</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">}</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">return</SPAN> <SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">array</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000cc">}</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">arr3</SPAN> <SPAN style="COLOR: #0000cc">=</SPAN> arrContentReplact<SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">arr</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #ff0000">echo</SPAN> <SPAN style="COLOR: #ff00ff">"&lt;pre&gt;"</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #ff0000">print_r</SPAN><SPAN style="COLOR: #0000cc">(</SPAN><SPAN style="COLOR: #0000ff">$</SPAN><SPAN style="COLOR: #008080">arr3</SPAN><SPAN style="COLOR: #0000cc">)</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN style="COLOR: #ff0000">echo</SPAN> <SPAN style="COLOR: #ff00ff">"&lt;/pre&gt;"</SPAN><SPAN style="COLOR: #0000cc">;</SPAN><BR><SPAN style="COLOR: #0000cc">?</SPAN><SPAN style="COLOR: #0000cc">&gt;</SPAN></FONT></SPAN></CODE></P></p>     <center><input type="image" onclick=copyToClipBoard() src="http://www.phpchina.com/images/phpcn_book_bu_tj.gif" border="0"></center>
页: [1]
查看完整版本: 使用递归替换数组中的内容