Several methods of turning Stdclass object objects into array in PHP

Source: Internet
Author: User
Tags foreach json php and

Analysis of Stdclass

Stdclass is one of several predefined classes in PHP and is a zent reserved class. It's actually a base class provided by PHP, is a blank class with nothing in it, we can instantiate it, then define a series of variables that pass through the variable (many PHP programmers use it to pass the values of a series of variables while they don't bother to create a class of their own). However, you can only pass properties because you cannot add a method after instantiating it. Because once a class is materialized, you cannot add a method.

Stdclass can be used as a base class, with the greatest feature being that (its derived classes) can automatically add member variables without being described in the definition.

All PHP variables are instances of Stdclass.

How to use:

1, the use of Stdclass:

$andy = Array ();
$andy = (object) $andy;
$andy->a = 1;
$andy->b = 2;
$andy->c = 3;

So the quantity A, B, C is filled in the stdclass. This is easy, because the new empty to the image but to $andy = new Andy; And you have to have a class andy{} first. Another example:


<?php
$a = new StdClass ();
$a->id = ' 11 ';
$a->username = ' me ';
Print_r ($a);
?>

will be output: StdClass Object ([id] => [username] => me).
Many times the use of this method instead of the array is just a different form of syntax.

Read:

StdClass Object
(
[Getweatherbycitynameresult] => stdClass Object
(
[String] => Array
(
[0] => Sichuan
[1] => Chengdu
[2] => 56294
[3] => 56294.jpg
[4] => 2009-5-17 13:52:08
[5] => 26 ℃/19 ℃
[6] => May 17 Cloudy Turn showers
)
)
)
In fact, the same as array, but the way of access to change a little bit, we are generally accustomed to using array[' key ' this way to access the array.
For this stdclass, as in the previous example, $weather->getweatherbycitynameresult->string[0] can access the property in this way, which will result in "Sichuan".

3, instantiation, new.

Compare the two codes:

<?php
$a = array (1=>2,2=>3);
$a = (object) $a;
$a->id = ' 11 ';
$a->username = ' me ';
Print_r ($a);
?>

Output: StdClass Object ([1] => 2 [2] => 3 [id] => one [username] => me.


<?php
$a = array (1=>2,2=>3);
$a = (object) $a;
$a = new StdClass ();
$a->id = ' 11 ';
$a->username = ' me ';
Print_r ($a);
?>

Output: StdClass Object ([id] => [username] => me).

Once instantiated with new, the preceding array is emptied, leaving behind the added, and if not instantiated, Stdclass will retain all elements.

It should be noted that in the function of the use of global, static when the new Stdclass reference, then &new Stdclass will be invalidated, should avoid using references, directly with the new Stdclass.


We often use API interface to get data back to the JSON value, often simply through the Json_decode method to get the value is not an array, but with Stdclass Objec object String, then if we want to get the corresponding PHP array, There are several ways to get this.


method One:

PHP StdClass object to 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;
}


Method Two:

$array = Json_decode (Json_encode (simplexml_load_string ($xmlString)), TRUE);


Method Three:

 function object2array_pre (& $object) {
        if Is_object ($ Object) {
            $arr = (array) ($object);
       } else {
             $arr = & $object;
       }
        if (Is_array ($arr)) {
             foreach ($arr as $varName => $varValue) {
                 $arr [$varName] = $this->object2array ($varValue);
           }
       }
        return $arr;
   }


If it is 10W of data, the implementation to 1s, the structure more complex, can reach 3s, poor performance
You can replace it with the following:

Function Object2array (& $object) {
$object = Json_decode (Json_encode ($object), true);
return $object;
}

But the characteristics of JSON, can only be directed at UTF8, otherwise you have to first transcoding.

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.