在数据结构中添加 stdClass 对象

发布于 2024-09-06 23:42:49 字数 287 浏览 2 评论 0原文

stdClass::__set_state(array(
   'zone1' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'id' => '123',
       'owner' => '234',
       ...
    )),

我的基础知识有点少,所以我遇到了麻烦...我需要创建上述结构,但我不知道如何...

stdClass::__set_state(array(
   'zone1' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'id' => '123',
       'owner' => '234',
       ...
    )),

My basics are a bit shot, so I'm having trouble with this... I need to create the above structure, but I'm not sure how to...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2024-09-13 23:42:49
$a = new stdclass;
$a->zone1 = array();
$a->zone1[0] = new stdclass;
$a->zone1[0]->id = "123";
$a->zone1[0]->owner = "234";

或者,依赖于数组在转换为对象时会转换为 stdClass 对象的事实:

$a = (object) array(
    "zone1" => array(
       (object) array("id" => "123", "owner" => "234"),
    ),
);

为此,var_export 给出:

stdClass::__set_state(array(
   'zone1' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'id' => '123',
       'owner' => '234',
    )),
  ),
))

请注意,丹尼尔是否已指出,stdClass 没有'实际上有一个 __set_state 方法。我想您只是通过给出 var_export 的输出来举例说明变量的结构。序列化应该使用 serialize 来完成。

$a = new stdclass;
$a->zone1 = array();
$a->zone1[0] = new stdclass;
$a->zone1[0]->id = "123";
$a->zone1[0]->owner = "234";

Alternatively, relying upon the fact that arrays are converted to stdClass objects when casted into objects:

$a = (object) array(
    "zone1" => array(
       (object) array("id" => "123", "owner" => "234"),
    ),
);

For this, var_export gives:

stdClass::__set_state(array(
   'zone1' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'id' => '123',
       'owner' => '234',
    )),
  ),
))

Note that, has Daniel has pointed out, stdClass doesn't actually have a __set_state method. I supposed you were just exemplifying the structure of the variable by giving the output of var_export. Serialization should be done with serialize instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文