English 中文(简体)
我怎么能够用星号或无效财产名称等名称来否定财产?
原标题:How can I access object properties with names like integers or invalid property names?

我正在使用<代码>json_decode(),例如:

$myVar = json_decode($data)

这使我产生这样的结果:

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

我想获得关键[0]的明显价值。 当我试图做这样的事情时:

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

我发现这一错误:

Parse差错: syntax误差,意料之外的T_DNUMBER

两个数字/推算器似乎存在问题。

最佳回答

Updated for PHP 7.2

PHP 7.2向做了一项行为改变,在标语和阵列中贴上数字钥匙,从而消除了这一特别不一致之处,并按预期举出以下所有例子。

One less thing to be confused about!


Original answer (applies to versions earlier than 7.2.0)

PHP在你直线的黑暗面上所占的份额 不要在内部发现。 标有编号的特性之一......

What they never told you

<><>Fact #1: 你们不能用不易法律变更的名称查阅财产。

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

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

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

http://www.ohchr.org。 但是,如果财产名称是所有数字,那么not!

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

rel=“noretinger”>Live example

<>0>Fact #4: 最好,除非物体先从阵列中排出。

$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?

Live example.

tty tu tu ??

What you can do

<>pstrong>Option #1:

最为实际的做法是将你感兴趣的物体重新归入一个阵列,使你能够获取这些财产:

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

不幸的是,这并不令人厌恶。 因此,就你而言,你需要做这样的事情:

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

Option #2: the nuclear option

另一种做法是写一种功能,将物体重新转换成阵列:

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

然而,我并不认为,这是整个方案上更好的选择,因为它将毫无必要地把你所关心的所有财产(not<>em>)和你们所关心的所有财产都划入。

Option #3: playing it clever

以前选择的另一种选择是利用初等干事的现有职能:

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

初专干事的职能有助于在无需界定任何外部职能的情况下重新转换成阵列。 不管怎样,它还是对选择2和附加的“nuke”不利。 下述不利之处:如果在物体内有任何座标,则这些座标在UTF-8上编码(这是json_encode<<<>>>>>。

问题回答

PHP 7

查阅标号为财产名称的不动产。

在投下一个阵列之后,多数需要反对。

    $arr = [2,3,7];
    $o = (object) $arr;

    $t = "1";
    $t2 = 1;
    $t3 = (1);

    echo $o->{1};     // 3
    echo $o->{ 1 };   // 3
    echo $o->$t;      // 3
    echo $o->$t2;     // 3
    echo $o->$t3;     // 3

    echo $o->1;       // Error
    echo $o->(1);     // Error

物体从@开始,如:

SimpleXMLElement Object (
    [@attributes] => Array (
        [href] => qwertyuiop.html
        [id] => html21
        [media-type] => application/xhtml+xml
    )
)

您必须使用:

print_r($parent_object->attributes());

;{@attributes } 或_0000_object[@attributes]

我不敢说,从数字开始就允许你点名物体。 从一封信起,将第一个“448364”改为“448364”。

第二组是阵列,用括号来查阅:

print myVar->highlighting->test_448364->Data[0]

而是

I had copied this function from the Internet. If it works as it says ("Function to Convert stdClass Objects to Multidimensional Arrays"), try the following:

<?php

    function objectToArray($d) {
        if (is_object($d)) {
            // Gets the properties of the given object
            // with get_object_vars function
            $d = get_object_vars($d);
        }

        if (is_array($d)) {
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return array_map(__FUNCTION__, $d);
        }
        else {
            // Return array
            return $d;
        }
    }

?>
  • first pass your array to objectToArray function
  • then take the return value
  • echo [highlighting][448364][Data][0]

资料来源:





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签