stdClass 对象问题

发布于 2024-08-07 13:48:50 字数 1061 浏览 2 评论 0原文

我正在努力使用 PHP 解析以下数据。 API 返回它,我尝试了各种语法。如何以非对象方式返回数据?或者,使用 stdClass 调用数据的语法是什么?

我可以将其转换为一个基于数据的数组,甚至两个吗?当谈到基于对象的数据集时,我迷失了方向。

stdClass Object
(
    [0] => stdClass Object
        (
            [district] => stdClass Object
                (
                    [state] => NY
                    [number] => 29
                )

        )

    [1] => stdClass Object
        (
            [district] => stdClass Object
                (
                    [state] => NY
                    [number] => 26
                )

        )

)

当我自己创建对象,然后执行 var_dump 时,我得到以下结果:

object(stdClass)#8 (2) {
  [0]=>
  object(stdClass)#4 (1) {
    ["district"]=>
    object(stdClass)#5 (2) {
      ["state"]=>
      string(2) "NY"
      ["number"]=>
      string(2) "29"
    }
  }
  [1]=>
  object(stdClass)#6 (1) {
    ["district"]=>
    object(stdClass)#7 (2) {
      ["state"]=>
      string(2) "NY"
      ["number"]=>
      string(2) "26"
    }
  }
}

I'm struggling to parse the below data using PHP. An API returns it, and I've tried various syntaxes. How do I return the data in a non-object way? Or, what's the syntax to call the data using the stdClass?

Could I convert this to one data based array, or even two? I'm lost when it comes to object-based data sets.

stdClass Object
(
    [0] => stdClass Object
        (
            [district] => stdClass Object
                (
                    [state] => NY
                    [number] => 29
                )

        )

    [1] => stdClass Object
        (
            [district] => stdClass Object
                (
                    [state] => NY
                    [number] => 26
                )

        )

)

When i create the object on my own, and then do a var_dump, I get this:

object(stdClass)#8 (2) {
  [0]=>
  object(stdClass)#4 (1) {
    ["district"]=>
    object(stdClass)#5 (2) {
      ["state"]=>
      string(2) "NY"
      ["number"]=>
      string(2) "29"
    }
  }
  [1]=>
  object(stdClass)#6 (1) {
    ["district"]=>
    object(stdClass)#7 (2) {
      ["state"]=>
      string(2) "NY"
      ["number"]=>
      string(2) "26"
    }
  }
}

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

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

发布评论

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

评论(6

情深缘浅 2024-08-14 13:48:50

他们可能在代码中将数组转换为对象($object = (object) $array)。这样做的优点是从现在开始它将通过引用传递(这是对象的默认设置),缺点是对象完全无用(成员不能以数字开头 - 请参阅 PHP 文档中的正则表达式),直到您将其转换回来(PHP 确实允许一些非常神秘的事情):

$array = (array) $bogusObject;
$array[0]->district->state === 'NY';

They are probably casting arrays to objects in their code ($object = (object) $array). This has the advantage that it will be passed by reference from now on (as is the default with objects) and the disadvantage that the object is completely useless (members cannot start with numbers - see the regex in PHP's docs) until you cast it back (PHP does allow some very mysterious things):

$array = (array) $bogusObject;
$array[0]->district->state === 'NY';
等数载,海棠开 2024-08-14 13:48:50

使用:

$object->{'0'}->district->state

基本上,您可以快捷地将字符串分配给变量,然后使用该变量作为对象访问器。

$zero = "0";
$object->$zero; /* or */ $object->{$zero};

Use:

$object->{'0'}->district->state

Basically You're short-cutting assigning a string to a variable, then using that variable as your object accessor.

$zero = "0";
$object->$zero; /* or */ $object->{$zero};
感情洁癖 2024-08-14 13:48:50

我现在正在查看他们的代码,不幸的是,他们没有在类中公开选项,以便您以关联数组树与 stdClass 对象树的形式请求数据。

“问题”位于 class.sunlightlabs.php 中的第 96 行,

return json_decode( $data );

您有几个选择。

  1. 只需使用 stdClass 语法即可。
  2. 将返回的 stdClass 树转换为关联数组 one

#1 in action

// echo the state of the 2nd object in the result
echo $result->{0}->district->state;

#2 in action

$result = toArray( $result );

function toArray( $data )
{
  if ( is_object( $data ) )
  {
    $data = get_object_vars( $data );
  }
  return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
}

您还可以通过一些创造性的模式应用直接使用他们的类,但是他们已经大量使用了子类,这使事情变得相当复杂,所以我坚持使用这两种解决方案之一。

I'm looking at their code now, and unfortunately, they've not exposed the option in their class for you to request the data as a associative array tree vs a stdClass object tree.

The "problem" is at line 96 in class.sunlightlabs.php

return json_decode( $data );

You have a couple options.

  1. Just use the stdClass syntax.
  2. Convert the returned stdClass tree into a associative array one

#1 in action

// echo the state of the 2nd object in the result
echo $result->{0}->district->state;

#2 in action

$result = toArray( $result );

function toArray( $data )
{
  if ( is_object( $data ) )
  {
    $data = get_object_vars( $data );
  }
  return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
}

You could also work with their class directly via some creative application of patterns, but they make heavy use of sublcasses already which complicates it quite a bit, so I'd stick to one of these two solutions.

自在安然 2024-08-14 13:48:50

您可以像这样迭代对象:

foreach ($obj as $each) {
    echo $each->district->state . ' - ' . $each->district->number . '<br />';
}

You can iterate through the object like this:

foreach ($obj as $each) {
    echo $each->district->state . ' - ' . $each->district->number . '<br />';
}
雪化雨蝶 2024-08-14 13:48:50

用户“null”在评论中建议了这一点,但想将其放在这里,这样就不会那么容易错过。

最好的选择是将 TRUE 作为 json_decode 中的第二个参数传递,即 json_decode($data, TRUE) ,这使其返回关联数组而不是类。因此,如果您有权访问源代码,请进行更改。

The user 'null' suggested this in the comments, but want to put it here so it's not missed so easly.

The best option is to pass TRUE as the second param in json_decode i.e. json_decode($data, TRUE) which makes it return associative arrays instead of classes. So if you have access to the source code – make that change.

吲‖鸣 2024-08-14 13:48:50

Soulmerge 的这个解决方案对我来说效果很好:

$array = (array) $bogusObject;
$array[0]->district->state === 'NY';

非常感谢!

This solution from soulmerge just worked fine for me:

$array = (array) $bogusObject;
$array[0]->district->state === 'NY';

Thanks a lot!!

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