PHP object and Array conversion ____php

Source: Internet
Author: User
1.function ToArray ($obj)
{
    if (Is_object ($obj)) $obj = (array) $obj;
    if (Is_array ($obj)) {
        $new = array ();
        foreach ($obj as $key => $val) {
            $new [$key] = ToArray ($val);
        }
    } else {
        $new = $obj;
    }

    return $new;
}
2.function Objecttoarray ($object) {
    if (!is_object ($object) &&!is_array ($object)) return
        $object; return

    array_map (' Objecttoarray ', (array) $object);
}
3. $object = (object) $array;
4. $object = new StdClass ();
foreach ($array as $key => $value)
{
    $object-> $key = $value;
}
5.//Cast to a array
$array = (array) $object;
5.//get_object_vars
$array = Get_object_vars ($object);
6./** * object to Array * ARRAY_MAP (__function__, $array); On each value in the array, each value is multiplied by itself, and the array with the new value is returned * Is_object ($object)? Get_object_vars ($object): $object;
        Returns an associative array of object properties * @param $object * @return array|void/public static function Objecttoarray ($object) {
        $object = (array) $object;
            foreach ($object as $k => $v) {if (GetType ($v) = = ' Resource ') {return; } if (GetType ($v) = = ' object ' | | GetType ($v) = = ' array ') {$object [$k] = (array) stringandarrayh
            Elper::objecttoarray ($v);
    } return $object; }
 7/** * Array Object * @param $array * @return object|void/public Stati
        C function Arraytoobject ($array) {if (GetType ($array)!= ' array ') {return;
                foreach ($array as $k => $v) {if (GetType ($v) = = ' Array ' | | GetType ($v) = = ' object ') {
            $array [$k] = (object) stringandarrayhelper::arraytoobject ($v);
    } return (object) $array; }

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.