A recursive implementation of the PHP array to the object using recursion, _php tutorial

Source: Internet
Author: User

Recursive implementation of the conversion between the PHP array and the object, with the array recursion


The example in this article describes the use of recursive implementations of PHP arrays with objects. Share to everyone for your reference. The implementation method is as follows:

Here are some simple questions about the conversion of objects to arrays, using recursion to write two methods as follows:

function Arraytoobject ($e) {     if (GetType ($e)! = ' array ') return;   foreach ($e as $k = + $v) {     if (GetType ($v) = = ' Array ' | | GetType ($v) = = ' object ')        $e [$k]= (Object) Arraytoobject ($ v);   }    Return (object) $e;}
function Objecttoarray ($e) {  $e = (array) $e;  foreach ($e as $k = + $v) {    if (GetType ($v) = = ' resource ') return;    if (GetType ($v) = = ' object ' | | GetType ($v) = = ' array ')      $e [$k]= (Array) Objecttoarray ($v);  }  return $e;}
function Object_to_array ($e) {   $_arr = Is_object ($e)? Get_object_vars ($e): $e;   foreach ($_arr as $key = $val) {     $val = (Is_array ($val) | | is_object ($val))? Object_to_array ($val): $val;     $arr [$key] = $val;   }   return $arr; }

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1021081.html www.bkjia.com true http://www.bkjia.com/PHPjc/1021081.html techarticle The recursive implementation of the PHP array with the object of the conversion method, the array recursive example of this article describes the PHP array and objects using recursive implementation of the method of conversion. Share to everyone for your reference ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.