Convert a PHP object to an associative array

后端 未结 30 1478
走了就别回头了
走了就别回头了 2020-11-22 02:18

I\'m integrating an API to my website which works with data stored in objects while my code is written using arrays.

I\'d like a quick-and-dirty function to convert

相关标签:
30条回答
  • 2020-11-22 02:30

    You can easily use this function to get the result:

    function objetToArray($adminBar){
        $reflector = new ReflectionObject($adminBar);
        $nodes = $reflector->getProperties();
        $out = [];
        foreach ($nodes as $node) {
            $nod = $reflector->getProperty($node->getName());
            $nod->setAccessible(true);
            $out[$node->getName()] = $nod->getValue($adminBar);
        }
        return $out;
    }
    

    Use PHP 5 or later.

    0 讨论(0)
  • 2020-11-22 02:31

    You can also create a function in PHP to convert an object array:

    function object_to_array($object) {
        return (array) $object;
    }
    
    0 讨论(0)
  • 2020-11-22 02:31

    This answer is only the union of the different answers of this post, but it's the solution to convert a PHP object with public or private properties with simple values or arrays to an associative array...

    function object_to_array($obj)
    {
        if (is_object($obj))
            $obj = (array)$this->dismount($obj);
        if (is_array($obj)) {
            $new = array();
            foreach ($obj as $key => $val) {
                $new[$key] = $this->object_to_array($val);
            }
        }
        else
            $new = $obj;
        return $new;
    }
    
    function dismount($object)
    {
        $reflectionClass = new \ReflectionClass(get_class($object));
        $array = array();
        foreach ($reflectionClass->getProperties() as $property) {
            $property->setAccessible(true);
            $array[$property->getName()] = $property->getValue($object);
            $property->setAccessible(false);
        }
        return $array;
    }
    
    0 讨论(0)
  • 2020-11-22 02:31

    Some impovements to the "well-knwon" code

    /*** mixed Obj2Array(mixed Obj)***************************************/ 
    static public function Obj2Array($_Obj) {
        if (is_object($_Obj))
            $_Obj = get_object_vars($_Obj);
        return(is_array($_Obj) ? array_map(__METHOD__, $_Obj) : $_Obj);   
    } // BW_Conv::Obj2Array
    

    Notice that if the function is member of a class (like above) you must change __FUNCTION__ to __METHOD__

    0 讨论(0)
  • 2020-11-22 02:35

    From the first Google hit for "PHP object to assoc array" we have this:

    function object_to_array($data)
    {
        if (is_array($data) || is_object($data))
        {
            $result = array();
            foreach ($data as $key => $value)
            {
                $result[$key] = object_to_array($value);
            }
            return $result;
        }
        return $data;
    }
    

    The source is at codesnippets.joyent.com.

    0 讨论(0)
  • 2020-11-22 02:35

    Here is my recursive PHP function to convert PHP objects to an associative array:

    // ---------------------------------------------------------
    // ----- object_to_array_recursive --- function (PHP) ------
    // ---------------------------------------------------------
    // --- arg1: -- $object  =  PHP Object         - required --
    // --- arg2: -- $assoc   =  TRUE or FALSE      - optional --
    // --- arg3: -- $empty   =  '' (Empty String)  - optional --
    // ---------------------------------------------------------
    // ----- Return: Array from Object --- (associative) -------
    // ---------------------------------------------------------
    
    function object_to_array_recursive($object, $assoc=TRUE, $empty='')
    {
        $res_arr = array();
    
        if (!empty($object)) {
    
            $arrObj = is_object($object) ? get_object_vars($object) : $object;
    
            $i=0;
            foreach ($arrObj as $key => $val) {
                $akey = ($assoc !== FALSE) ? $key : $i;
                if (is_array($val) || is_object($val)) {
                    $res_arr[$akey] = (empty($val)) ? $empty : object_to_array_recursive($val);
                }
                else {
                    $res_arr[$akey] = (empty($val)) ? $empty : (string)$val;
                }
                $i++;
            }
        }
        return $res_arr;
    }
    
    // ---------------------------------------------------------
    // ---------------------------------------------------------
    

    Usage example:

    // ---- Return associative array from object, ... use:
    $new_arr1 = object_to_array_recursive($my_object);
    // -- or --
    // $new_arr1 = object_to_array_recursive($my_object, TRUE);
    // -- or --
    // $new_arr1 = object_to_array_recursive($my_object, 1);
    
    
    // ---- Return numeric array from object, ... use:
    $new_arr2 = object_to_array_recursive($my_object, FALSE);
    
    0 讨论(0)
提交回复
热议问题