stdClass对象解决问题 [英] stdClass Object problems

查看:136
本文介绍了stdClass对象解决问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我挣扎解析使用PHP下面的数据。一个API返回它,我已经试过各种语法。如何在一个非对象的方式返回的数据?或者说,什么叫使用stdClass的数据的语法?

我能否将其转换为基于一个数据阵列,甚至两个?当涉及到基于对象的数据集,我迷路了。

  stdClass的对象

    [0] => stdClass的对象
        (
            [区域] => stdClass的对象
                (
                    [状态] =>纽约
                    [数字] => 29
                )        )    [1] => stdClass的对象
        (
            [区域] => stdClass的对象
                (
                    [状态] =>纽约
                    [数字] => 26
                )        ))

当我创建我自己的目标,然后做的var_dump,我得到这样的:

 对象(stdClass的)#8(2){
  [0] =>
  对象(stdClass的)#4(1​​){
    [区] =>
    对象(stdClass的)#5(2){
      [状态] =>
      字符串(2)NY
      [数量] =>
      串(2)29
    }
  }
  [1] =>
  对象(stdClass的)#6(1){
    [区] =>
    对象(stdClass的)#7(2){
      [状态] =>
      字符串(2)NY
      [数量] =>
      字符串(2)26
    }
  }
}


解决方案

他们可能铸造阵列在其code(对象 $对象=(对象)$数组)。这样做,这将参照从现在开始传递的优点(如与对象的默认)和缺点的目标是完全无用的(成员不能以数字开头 - 看的正则表达式在PHP的文档),直到你将它转换回(PHP确实让一些很神秘的东西):

  $阵列=(阵列)$ bogusObject;
$数组[0] - >&区 - GT;状态==='NY';

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"
    }
  }
}

解决方案

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';

这篇关于stdClass对象解决问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆