Heim >Backend-Entwicklung >PHP-Tutorial > 多维数组递归有关问题

多维数组递归有关问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 13:49:431321Durchsuche

多维数组递归问题

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
//为了实现内容中去除HTML A标签
function htmla(&$string)
    {
        if(is_array($string))
        {
            foreach($string as $k=>$v)
            {
                $string[$k]=preg_replace("//","",$v);
                if(is_array($v))
                {
                    htmla($string[$k]);
                }
            }
        }else{
            $string=preg_replace("//","",$string);
        }
    }

    $test=array(1,2,34,5,array("a"=>"<a href="">而</a>",1,23,array("df"=>"<a href="">测试下</a>"),array(23,32)),"b"=>"<a href="">三</a>");
        htmla($test);
        var_dump($test);
//结果
array(6) {
  [0] => string(1) "1"
  [1] => string(1) "2"
  [2] => string(2) "34"
  [3] => string(1) "5"
  [4] => array(5) {
    ["a"] => string(3) "而"
    [0] => string(1) "1"
    [1] => string(2) "23"
    [2] => string(5) "Array" //超过2维就没法转换了。。
    [3] => string(5) "Array"
  }
  ["b"] => string(3) "三"
}



------解决方案--------------------
foreach($string as $k=>$v)
{
//$string[$k]=preg_replace("//","",$v);
if(is_array($v))
{
htmla($string[$k]);
}
else
{
$string[$k]=preg_replace("//","",$v);
}
}
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel: 怎么将变量插入字符串 Nächster Artikel: php、该怎么处理