Php array and object conversion method

Source: Internet
Author: User
Php array and object conversion method functionarrayToObject ($ e) {if (gettype ($ e )! & Amp; 39; array & amp; 39;) return; foreach ($ eas $ k & gt; $ v) {if (gettype ($ v) & amp; 39

Php array and object conversion method

  1. Function arrayToObject ($ e ){
  2. If (gettype ($ e )! = 'Array') return;
  3. Foreach ($ e as $ k => $ v ){
  4. If (gettype ($ v) = 'array' | getType ($ v) = 'object ')
  5. $ E [$ k] = (object) arrayToObject ($ v );
  6. }
  7. Return (object) $ e;
  8. }
  9.  
  10. Function objectToArray ($ e ){
  11. $ E = (array) $ e;
  12. Foreach ($ e as $ k => $ v ){
  13. If (gettype ($ v) = 'resource') return;
  14. If (gettype ($ v) = 'object' | gettype ($ v) = 'array ')
  15. $ E [$ k] = (array) objectToArray ($ v );
  16. }
  17. Return $ e;
  18. }

For how to convert arrays to objects, see some posts on phpx.

  1. $ Andy = array ();
  2. $ Andy ['A'] = 1;
  3. $ Andy ['B'] = 2;
  4. $ Andy ['c'] = 3;

To forcibly convert andy to an array, it seems that the reference is more convenient, mainly because it is->

  1. $ Andy = array ();
  2. $ Andy = (object) $ andy;
  3. $ Andy-> a = 1;
  4. $ Andy-> B = 2;
  5. $ Andy-> c = 3;

It is also troublesome, and StdClass is used again

  1. $ Andy = new StdClass;
  2. $ Andy-> a = 1;
  3. $ Andy-> B = 2;
  4. $ Andy-> c = 3;

StdClass is a base class of PHP. almost all classes inherit this class, so it can be new at any time. This variable can be made an object. at the same time, this base class has another special feature, that is, there is no method. here, there is also a problem with the consumption of objects and array resources: the uniqueness of the PHP5 object, the object is called anywhere, these parameters reference the address type, so the resources consumed are slightly less. when assigning values to the address type on other pages, they are directly modified instead of referencing a copy, the array should be the most convenient in the use of the program. However, every time the array is referenced ($ a = $ B), a copy is actually created, and after the array is unset, it still occupies the memory (not tested), and there is also a function arrayobject, which can directly convert the array into an object. this is also a good way.

  1. ArrayObject ::__ construct -- Construct a new array object
  2. $ Array = array ('1' => 'one ',
  3. '2' => 'two ',
  4. '3' => 'Three ');
  5. $ Arrayobject = new ArrayObject ($ array );
  6. Print_r ($ arrayobject );
  7. ?>
  8. ArrayObject Object
  9. (
  10. [1] => one
  11. [2] => two
  12. [3] => three
  13. )

After testing, the object can be converted to an array or forced conversion:

$ Arr = (array) $ obj;

The conversion result of the following example can be compared:

  1. Tag Object
  2. (
  3. [Name] => main
  4. [Text] =>
  5. [Parrent] =>
  6. [Content] =>
  7. [Props] => Array
  8. (
  9. )
  10.  
  11. [Inner] => Array
  12. (
  13. )
  14.  
  15. [Level] => 0
  16. )
  17.  
  18. Array
  19. (
  20. [Name] => main
  21. [Text] =>
  22. [Parrent] =>
  23. [Content] =>
  24. [Props] => Array
  25. (
  26. )
  27.  
  28. [Inner] => Array
  29. (
  30. )
  31.  
  32. [Level] => 0
  33. )

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.