Home  >  Article  >  Backend Development  >  Introduction to specific code writing for PHP array conversion_PHP tutorial

Introduction to specific code writing for PHP array conversion_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:29:17818browse

When you actually use PHP code, you may be familiar with The following is the specific code for PHP array conversion:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN> ?php      </SPAN></SPAN><LI class=""><SPAN>class arrayiconv      </SPAN><LI class=alt><SPAN>{      </SPAN><LI class=""><SPAN>static protected $in;      </SPAN><LI class=alt><SPAN>static protected $out;      </SPAN><LI class=""><SPAN>/**     </SPAN><LI class=alt><SPAN>  * 静态方法,该方法输入数组并返回数组     </SPAN><LI class=""><SPAN>  *     </SPAN><LI class=alt><SPAN>  * @param unknown_type $array 输入的数组     </SPAN><LI class=""><SPAN>  * @param unknown_type $in 输入数组的编码     </SPAN><LI class=alt><SPAN>  * @param unknown_type $out 返回数组的编码     </SPAN><LI class=""><SPAN>  * @return unknown 返回的数组     </SPAN><LI class=alt><SPAN>  */     </SPAN><LI class=""><SPAN>static public function Conversion($array,$in,$out)      </SPAN><LI class=alt><SPAN>{      </SPAN><LI class=""><SPAN>  self::$</SPAN><SPAN class=attribute><FONT color=#ff0000>in</FONT></SPAN><SPAN>=$in;      </SPAN></SPAN><LI class=alt><SPAN>  self::$</SPAN><SPAN class=attribute><FONT color=#ff0000>out</FONT></SPAN><SPAN>=$out;      </SPAN></SPAN><LI class=""><SPAN>  return self::arraymyicov($array);      </SPAN><LI class=alt><SPAN>}      </SPAN><LI class=""><SPAN>/**     </SPAN><LI class=alt><SPAN>  * 内部方法,循环数组     </SPAN><LI class=""><SPAN>  *     </SPAN><LI class=alt><SPAN>  * @param unknown_type $array     </SPAN><LI class=""><SPAN>  * @return unknown     </SPAN><LI class=alt><SPAN>  */     </SPAN><LI class=""><SPAN>static private function arraymyicov($array)      </SPAN><LI class=alt><SPAN>{      </SPAN><LI class=""><SPAN>  foreach ($array as $</SPAN><SPAN class=attribute><FONT color=#ff0000>key</FONT></SPAN><SPAN>=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span>$value)      </span></span></li>
<li class="alt"><span>  {      </span></li>
<li class="">
<span>   $</span><span class="attribute"><font color="#ff0000">key</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">self</font></span><span>::myiconv($key);      </span>
</li>
<li class="alt"><span>   if (!is_array($value)) {      </span></li>
<li class="">
<span>    $</span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">self</font></span><span>::myiconv($value);      </span>
</li>
<li class="alt"><span>   }else {      </span></li>
<li class="">
<span>    $</span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">self</font></span><span>::arraymyicov($value);      </span>
</li>
<li class="alt"><span>   }      </span></li>
<li class=""><span>   $temparray[$key]=$value;      </span></li>
<li class="alt"><span>  }      </span></li>
<li class=""><span>  return $temparray;      </span></li>
<li class="alt"><span>}      </span></li>
<li class=""><span>/**     </span></li>
<li class="alt"><span>  * 替换数组编码     </span></li>
<li class=""><span>  *     </span></li>
<li class="alt"><span>  * @param unknown_type $str     </span></li>
<li class=""><span>  * @return unknown     </span></li>
<li class="alt"><span>  */     </span></li>
<li class=""><span>static private function myiconv($str)      </span></li>
<li class="alt"><span>{      </span></li>
<li class=""><span>  return iconv(self::$in,self::$out,$str);      </span></li>
<li class="alt"><span>}      </span></li>
<li class=""><span>}      </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">b</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("测试"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>array("测试"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"测试","测试"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"测试","测试"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>array("测试"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"测试")),"fasdf"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"测试","测试"=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"测试 ");      </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">a</font></span><span> =</span><span class="attribute-value"><font color="#0000ff">arrayiconv</font></span><span>::Conversion($b,"utf-8","gb2312");      </span>
</li>
<li class="alt"><span>print_r($a);      </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>    </span>
</li>
</ol>

The above code is the specific implementation method of PHP array conversion. I hope it will be helpful to my friends.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446374.htmlTechArticleWhen you actually use PHP code, you may be familiar with the following specific code for PHP array conversion: ?php classarrayiconv { staticprotected $in; staticprotected$out; /** *Static method, this method...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn