Convert a PHP object to an associative array

后端 未结 30 1578
走了就别回头了
走了就别回头了 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:36

    By using typecasting you can resolve your problem. Just add the following lines to your return object:

    $arrObj = array(yourReturnedObject);
    

    You can also add a new key and value pair to it by using:

    $arrObj['key'] = value;
    
    0 讨论(0)
  • 2020-11-22 02:36

    I use this (needed recursive solution with proper keys):

        /**
         * This method returns the array corresponding to an object, including non public members.
         *
         * If the deep flag is true, is will operate recursively, otherwise (if false) just at the first level.
         *
         * @param object $obj
         * @param bool $deep = true
         * @return array
         * @throws \Exception
         */
        public static function objectToArray(object $obj, bool $deep = true)
        {
            $reflectionClass = new \ReflectionClass(get_class($obj));
            $array = [];
            foreach ($reflectionClass->getProperties() as $property) {
                $property->setAccessible(true);
                $val = $property->getValue($obj);
                if (true === $deep && is_object($val)) {
                    $val = self::objectToArray($val);
                }
                $array[$property->getName()] = $val;
                $property->setAccessible(false);
            }
            return $array;
        }
    

    Example of usage, the following code:

    class AA{
        public $bb = null;
        protected $one = 11;
    
    }
    
    class BB{
        protected $two = 22;
    }
    
    
    $a = new AA();
    $b = new BB();
    $a->bb = $b;
    
    var_dump($a)
    

    Will print this:

    array(2) {
      ["bb"] => array(1) {
        ["two"] => int(22)
      }
      ["one"] => int(11)
    }
    
    
    0 讨论(0)
  • 2020-11-22 02:38

    You can quickly convert deeply nested objects to associative arrays by relying on the behavior of the JSON encode/decode functions:

    $array = json_decode(json_encode($nested_object), true);
    
    0 讨论(0)
  • 2020-11-22 02:38

    You might want to do this when you obtain data as objects from databases:

    // Suppose 'result' is the end product from some query $query
    
    $result = $mysqli->query($query);
    $result = db_result_to_array($result);
    
    function db_result_to_array($result)
    {
        $res_array = array();
    
        for ($count=0; $row = $result->fetch_assoc(); $count++)
            $res_array[$count] = $row;
    
        return $res_array;
    }
    
    0 讨论(0)
  • 2020-11-22 02:39

    There's my proposal, if you have objects in objects with even private members:

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

    Type cast your object to an array.

    $arr =  (array) $Obj;
    

    It will solve your problem.

    0 讨论(0)
提交回复
热议问题