php将json对象转为数组

封装方法如下:

//json对象转数组
function json_to_array($json_str)
{
    $json_str1 = $json_str;
    if (is_array($json_str) || is_object($json_str)) {
        $json_str = $json_str1;
    } else if (is_null(json_decode($json_str))) {
        $json_str = $json_str1;
    } else {
        $json_str = strval($json_str);
        $json_str = json_decode($json_str, true);
    }
    $json_arr = array();
    foreach ($json_str as $k => $w) {
        if (is_object($w)) {
            $json_arr[$k] = json_to_array($w); //判断类型是不是object
        } else if (is_array($w)) {
            $json_arr[$k] = json_to_array($w);
        } else {
            $json_arr[$k] = $w;
        }
    }
    return $json_arr;
}
发布了120 篇原创文章 · 获赞 433 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/msllws/article/details/104743700