使用递归将html_entity_decode应用于对象和数组

Mar*_*own 1 php

我正在尝试构建一个将html_entity_decode应用于对象和数组的函数.由于我事先不知道结构,并且子属性也可以是对象或数组,因此简单的递归函数似乎是要走的路.我无法弄清楚为什么以下不起作用:

function decode($data){  
    if(is_object($data) || is_array($data)){
        foreach($data as &$value)
            $value = $this->decode($value);
    }
    else $data = html_entity_decode($data);

    return $data;
}
Run Code Online (Sandbox Code Playgroud)

我也试过以下,但也不起作用:

function decode($data){
    if(is_object($data))
        $data = get_object_vars($data);

    $data = is_array($data) ? array_map(array('MyClassName', 'decode'), $data) : html_entity_decode($data);

    return $data;
}
Run Code Online (Sandbox Code Playgroud)

这两种功能都不会对数据产生任何影响.我究竟做错了什么?

Bab*_*aba 9

主要问题是你正在尝试使用像数组一样的对象,is_object($data) || is_array($data)除非你将其转换object为array

其他问题基于不正确的变量名称而不返回正确的变量您可以尝试

$std = new stdClass();
$std->title = array("x" => "<a>XXX</a>","y" => "<b>YYY</b>");
$std->body = "<p> THis is the Body </p>";

$var = array(
        "a" => "I'll \"walk\" the <b>dog</b> now",
        "b" => array("<b>Hello World</b>",array(array("Yes am <strong> baba </strong>"))),
        "c" => $std

);

$class = new MyClassName();
$encode = $class->encode($var); // encode 
$decode = $class->decode($encode);  // decode it back

print_r($encode);
print_r($decode);
Run Code Online (Sandbox Code Playgroud)

编码数组

Array
(
    [a] => I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now
    [b] => Array
        (
            [0] => &lt;b&gt;Hello World&lt;/b&gt;
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => Yes am &lt;strong&gt; baba &lt;/strong&gt;
                        )

                )

        )

    [c] => stdClass Object
        (
            [title] => Array
                (
                    [x] => &lt;a&gt;XXX&lt;/a&gt;
                    [y] => &lt;b&gt;YYY&lt;/b&gt;
                )

            [body] => &lt;p&gt; THis is the Body &lt;/p&gt;
        )

)
Run Code Online (Sandbox Code Playgroud)

解码数组

Array
(
    [a] => I'll "walk" the <b>dog</b> now
    [b] => Array
        (
            [0] => <b>Hello World</b>
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => Yes am <strong> baba </strong>
                        )

                )

        )

    [c] => stdClass Object
        (
            [title] => Array
                (
                    [x] => <a>XXX</a>
                    [y] => <b>YYY</b>
                )

            [body] => <p> THis is the Body </p>
        )

)
Run Code Online (Sandbox Code Playgroud)

观看现场演示

class MyClassName {
    function encode($data) {
        if (is_array($data)) {
            return array_map(array($this,'encode'), $data);
        }
        if (is_object($data)) {
            $tmp = clone $data; // avoid modifing original object
            foreach ( $data as $k => $var )
                $tmp->{$k} = $this->encode($var);
            return $tmp;
        }
        return htmlentities($data);
    }

    function decode($data) {
        if (is_array($data)) {
            return array_map(array($this,'decode'), $data);
        }
        if (is_object($data)) {
            $tmp = clone $data; // avoid modifing original object
            foreach ( $data as $k => $var )
                $tmp->{$k} = $this->decode($var);
            return $tmp;
        }
        return html_entity_decode($data);
    }
}
Run Code Online (Sandbox Code Playgroud)