Convert PHP object to associative array

2018-12-31 05:31发布

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 an object to an array.

标签: php arrays
27条回答
深知你不懂我心
2楼-- · 2018-12-31 06:13

If your object properties are public you can do:

$array =  (array) $object;

If they are private or protected, they will have weird key names on the array. So, in this case you will need the following function:

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;
}
查看更多
一个人的天荒地老
3楼-- · 2018-12-31 06:13

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 in 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;
}
查看更多
墨雨无痕
4楼-- · 2018-12-31 06:16

this function can convert object proprity to array associatif

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 >= php5

查看更多
高级女魔头
5楼-- · 2018-12-31 06:16

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

$arrObj = array(yourReturnedObject);

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

$arrObj['key'] = value;
查看更多
素衣白纱
6楼-- · 2018-12-31 06:17

Hi,

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

// --------------------------------------------------------- 
// ----- object_to_array_recusive --- 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_recusive ( $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_recusive($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_recusive($my_object); 
// -- or -- 
// $new_arr1 = object_to_array_recusive($my_object,TRUE); 
// -- or -- 
// $new_arr1 = object_to_array_recusive($my_object,1); 


// ---- return numeric array from object, ... use: 
$new_arr2 = object_to_array_recusive($my_object,FALSE); 
查看更多
无色无味的生活
7楼-- · 2018-12-31 06:18

Type cast your object to an array.

$arr =  (array) $Obj;

It will solve your problem.

查看更多
登录 后发表回答