如何使用名称(如整数)访问对象属性? [英] How to access object properties with names like integers?

查看:105
本文介绍了如何使用名称(如整数)访问对象属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用json_decode()之类的东西:

I am using json_decode() something like:

$myVar = json_decode($data)

哪个给我这样的输出:

[highlighting] => stdClass Object
        (
            [448364] => stdClass Object
                (
                    [Data] => Array
                        (
                            [0] => Tax amount liability is ....... 

我想访问键[0]中的字符串值.当我尝试做类似的事情时:

I want to access the string value in the key [0]. When I try to do something like:

print $myVar->highlighting->448364->Data->0;

我收到此错误:

解析错误:语法错误,意外的T_DNUMBER

Parse error: syntax error, unexpected T_DNUMBER

两个数字/整数似乎有问题.

The two numerals/integers there seems to be problem.

推荐答案

已针对PHP 7.2更新

PHP 7.2引入了对在对象和数组类型转换中转换数字键的行为更改,这种特殊的不一致,并使得以下所有示例的行为均符合预期.

Updated for PHP 7.2

PHP 7.2 introduced a behavioral change to converting numeric keys in object and array casts, which fixes this particular inconsistency and makes all the following examples behave as expected.

还有一件值得困惑的事情!

One less thing to be confused about!

PHP有其自己的黑暗小巷,您真的不想在里面发现自己.名称为数字的对象属性就是其中之一...

PHP has its share of dark alleys that you really don't want to find yourself inside. Object properties with names that are numbers is one of them...

事实1:您不能轻松访问名称不是合法变量名称的属性

Fact #1: You cannot access properties with names that are not legal variable names easily

$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->123foo; // error

事实2:可以使用大括号语法访问此类属性

Fact #2: You can access such properties with curly brace syntax

$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->{'123foo'}; // OK!

事实3:,但是如果属性名称为全数字,则不是

Fact #3: But not if the property name is all digits!

$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
echo $o->{'123foo'}; // OK!
echo $o->{'123'}; // error!

实时示例 .

事实4:好吧,除非对象最初不是来自数组.

Fact #4: Well, unless the object didn't come from an array in the first place.

$a = array('123' => '123');
$o1 = (object)$a;
$o2 = new stdClass;
$o2->{'123'} = '123'; // setting property is OK

echo $o1->{'123'}; // error!
echo $o2->{'123'}; // works... WTF?

实时示例 .

很直观,你不同意吗?

选项1:手动完成

最实用的方法只是将您感兴趣的对象重新投射到数组中,这将允许您访问属性:

The most practical approach is simply to cast the object you are interested in back into an array, which will allow you to access the properties:

$a = array('123' => '123', '123foo' => '123foo');
$o = (object)$a;
$a = (array)$o;
echo $o->{'123'}; // error!
echo $a['123']; // OK!

不幸的是,这不是递归的.因此,在您的情况下,您需要执行以下操作:

Unfortunately, this does not work recursively. So in your case you 'd need to do something like:

$highlighting = (array)$myVar->highlighting;
$data = (array)$highlighting['448364']->Data;
$value = $data['0']; // at last!

选择#2:核选择

另一种方法是编写一个将对象递归转换为数组的函数:

An alternative approach would be to write a function that converts objects to arrays recursively:

function recursive_cast_to_array($o) {
    $a = (array)$o;
    foreach ($a as &$value) {
        if (is_object($value)) {
            $value = recursive_cast_to_array($value);
        }
    }

    return $a;
}

$arr = recursive_cast_to_array($myVar);
$value = $arr['highlighting']['448364']['Data']['0'];

但是,我不认为这是一个更好的选择,因为它会不必要地将所有您不感兴趣的属性以及您所感兴趣的所有属性强制转换为数组.

However, I 'm not convinced that this is a better option across the board because it will needlessly cast to arrays all of the properties that you are not interested in as well as those you are.

选项3:发挥聪明

上一个选项的替代方法是使用内置的JSON函数:

An alternative of the previous option is to use the built-in JSON functions:

$arr = json_decode(json_encode($myVar), true);
$value = $arr['highlighting']['448364']['Data']['0'];

JSON函数有助于执行到数组的递归转换,而无需定义任何外部函数.无论看起来多么理想,它都具有选项#2 的"nuke"劣势,此外,的劣势是,如果对象中有任何字符串,则必须将这些字符串 编码为UTF-8(这是 json_encode 的要求)

The JSON functions helpfully perform a recursive conversion to array without the need to define any external functions. However desirable this looks, it has the "nuke" disadvantage of option #2 and additionally the disadvantage that if there is any strings inside your object, those strings must be encoded in UTF-8 (this is a requirement of json_encode).

这篇关于如何使用名称(如整数)访问对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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