【问题标题】:A faster way of doing objectToArray一种更快的方法来做 objectToArray
【发布时间】:2013-09-28 23:10:17
【问题描述】:

我得到了下面这段代码的 sn-p,它工作得很好。我一直在分析它,并且这段代码被使用了很多次,所以我想尝试弄清楚如何以一种比当前编写方式更好的方式编写它。

有没有更有效的方法来写这个?

function objectToArray($d) {
    if (is_object($d)) {
        // Gets the properties of the given object
        // with get_object_vars function
        $d = get_object_vars($d);
    }

    if (is_array($d)) {
        // Return array converted to object Using __FUNCTION__ (Magic constant) for recursive call
        return array_map(__FUNCTION__, $d);
    }
    else {
        // Return array
        return $d;
    }
}

【问题讨论】:

  • 转成json,再转成关联数组?

标签: php performance


【解决方案1】:

您可以为需要转换的类实现toArray() 方法:

例如

class foo
{
  protected $property1;
  protected $property2;

  public function __toArray()
  {
    return array(
      'property1' => $this->property1,
      'property2' => $this->property2
    );
  }
 }

在我看来,访问受保护的属性并将整个转换封装在类中是最好的方法。

更新

需要注意的一点是,get_object_vars() 函数只会返回可公开访问的属性 - 可能不是您想要的。

如果上面的任务过于手动,那么来自类外的准确方法是使用ReflectionClass中内置的PHP(SPL):

$values = array();
$reflectionClass = new \ReflectionClass($object);
foreach($reflectionClass->getProperties() as $property) {
  $values[$property->getName()] = $property->getValue($object); 
}
var_dump($values);

【讨论】:

  • 谢谢 AlexP,那能代替当前的 sn-p 吗?
  • @ChrisEdington 看看我的更新。任何一个选项都可以,这取决于您尝试从中访问变量的范围以及向类添加新方法的可行性。
  • 是否能够保持相同的函数结构和名称,而只是重新编写内部代码?被转换的对象是一个 SOAP 响应,如果这有帮助吗?..
  • 是的,你需要做的只是添加一个方法,正如解释的那样,不需要对类进行其他更改。但是,每次添加新属性时都需要添加到数组中-除非您动态生成数组-这应该很容易(可以直接访问$this)。
  • 可能类似于使用 $array = json_decode(json_encode($object), true);在那个函数结构中?
【解决方案2】:

取决于它是什么类型的对象,许多标准的php对象都有内置的方法来转换它们

例如 MySQLi 结果可以这样转换

$resultArray = $result->fetch_array(MYSQLI_ASSOC);

如果它是一个自定义类对象,您可能会考虑在该类中实现一个方法,如 AlexP 所建议的那样

【讨论】:

  • 是否能够保持相同的函数结构和名称,而只是重新编写内部代码?被转换的对象是一个 SOAP 响应,如果这有帮助?..
  • 不熟悉 SOAP,但一个快速的 google 谴责它的 XML,因此您可以使用 PHP 的 XML 函数$soapResponce = simplexml_load_file("soapResponce.xml"); $soapArray = unserialize(serialize(json_decode(json_encode((array) $soapResponce), 1))); print_r($xml_array); credit: stackoverflow.com/questions/12148662/xml-to-array-php
【解决方案3】:

最终选择:

function objectToArray($d) {
$d = (object) $d;
return $d;
}
function arrayToObject($d) {
$d = (array) $d;
return $d;
}

【讨论】:

    【解决方案4】:

    正如 AlexP 所说,您可以实现方法 __toArray()。替代 ReflexionClass(复杂且昂贵),利用object iteration properties,您可以迭代$this,如下所示

    class Foo
    {
      protected $var1;
      protected $var2;
    
      public function __toArray()
      {
        $result = array();
        foreach ($this as $key => $value) {
          $result[$key] = $value;
        }
        return $result;
      }
    }
    

    这也将迭代未在类中定义的对象属性:例如

    $foo = new Foo;
    $foo->var3 = 'asdf';
    var_dump($foo->__toArray());)
    

    参见示例http://3v4l.org/OnVkf

    【讨论】:

      【解决方案5】:

      这是我发现的将对象转换为数组的最快方法。也适用于胶囊。

           function objectToArray ($object) {
              return json_decode(json_encode($object, JSON_FORCE_OBJECT), true);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多