Home > Backend Development > PHP Tutorial > PHP 将json的stdClass Object转成数组array的方法

PHP 将json的stdClass Object转成数组array的方法

PHPz
Release: 2018-09-30 15:45:27
forward
1032 people have browsed it

PHP 将json的stdClass Object转成数组array 方法二

原来发的有个方法一的函数

function object_array($array){
  if(is_object($array)){
    $array = (array)$array;
  }
  if(is_array($array)){
    foreach($array as $key=>$value){
      $array[$key] = object_array($value);
    }
  }
  return $array;
}
Copy after login

感觉太麻烦了 其实我一般都是用的方法二 因为不需要单独写函数

//$user是一个stdClass  每次调用都是 $user->uid  其实也是挺方便的
//把$user转为array数组
$user  =json_encode($user); //把她转换为json字符串
$user = json_decode($user,true); //再把json字符串格式化为数组
//或者直接是:
$user = json_decode(json_encode($user),true);
Copy after login

更多相关教程请访问 php编程从入门到精通全套视频教程

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template