将数组投射到对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将数组投射到对象相关的知识,希望对你有一定的参考价值。

  1. <?php
  2. /**
  3.  * Note This has been tested on PHP5
  4.  * I haven't tried it on PHP4
  5.  */
  6. $arr = array(
  7. "name" => "Jamie Allison",
  8. "age" => "31"
  9. );
  10. // This is all that is required to cast an array to an object.
  11. $data = (object)$arr;
  12.  
  13.  
  14. // Next create an object manually, with the same data.
  15. // For comparison purposes below.
  16. $test = new StdClass;
  17. $test->name = "Jamie Allison";
  18. $test->age = "31";
  19.  
  20. // Print out the Results, first dump the manually created class
  21. print "Manually created StdClass" . PHP_EOL . print_r($test, true) . PHP_EOL;
  22.  
  23. // Print out the Array Cast as an object.
  24. print "Array cast to an object" . PHP_EOL . print_r($data, true) . PHP_EOL;
  25. ?>

以上是关于将数组投射到对象的主要内容,如果未能解决你的问题,请参考以下文章

将特定对象投射到另一个对象

使用 Codable 投射到对象

如何将对象投射到桌子上?

将 activeX 对象投射到我的对象上的问题

将对象投射到 T

将 Firebase 快照投射到 Swift 3 对象