Heim  >  Artikel  >  Backend-Entwicklung  >  php数组与对象转换小例子

php数组与对象转换小例子

WBOY
WBOYOriginal
2016-07-25 09:11:54828Durchsuche

php数组与对象转换

例子:

  1. //php 对象到数组转换
  2. private function objToArr($obj){
  3. if(!is_object($obj) && !is_array($obj)) {
  4. return $obj;
  5. }
  6. $arr = array();
  7. foreach($obj as $k => $v){
  8. $arr[$k] = $this->objToArr($v);
  9. }
  10. return $arr;
  11. } // bbs.it-home.org
  12. //简单实现json到php数组转换功能
  13. private function simple_json_parser($json){
  14. $json = str_replace("{","",str_replace("}","", $json));
  15. $jsonValue = explode(",", $json);
  16. $arr = array();
  17. foreach($jsonValue as $v){
  18. $jValue = explode(":", $v);
  19. $arr[str_replace('"',"", $jValue[0])] = (str_replace('"', "", $jValue[1]));
  20. }
  21. return $arr;
  22. }
复制代码


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