如何将PHP数组转换为JSON数组? [关闭]

如何将PHP数组转换为JSON数组?  [关闭]

问题描述:

For example, I have the following code in JSON:

{
    "tag": "img",
    "attr": [
{
        "alt": "text",
        "src": "url",
        "void": "true",
        "all": "true"
}
]
}

What PHP code should I use to echo/print the code above? I tried this:

$arr = array (
    "tag" => "img",
$attr = array (
    "alt" => "text",
    "src" => "url",
    "void" => "true",
    "all" => "true"
)
);

And then:

echo $arr = json_encode($arr);

But I get error. Any ideas?

Your array declaration is wrong. It should be:

$arr = array (
    "tag" => "img",
    "attr" => array (
        "alt" => "text",
        "src" => "url",
        "void" => "true",
        "all" => "true"
    )
);

Change to -

$arr = array (
    "tag" => "img",
    "attr" => array (
        "alt" => "text",
        "src" => "url",
        "void" => "true",
        "all" => "true"
    )
);

ie. $attr = array ( to "attr" => array (