【问题标题】:Problem accessing the value of the json with a point in its name [duplicate]使用名称中的点访问 json 的值时出现问题 [重复]
【发布时间】:2019-09-27 05:56:54
【问题描述】:

我需要访问名称中包含点的 json 值。

我想访问“proy_sim.name”字段,但我不知道如何

{        
    "prsp_sol": [
        {
            "proy_sim.name": "Vehículos",
            "prsp_def.name": "TRACTOR"  
        }
    ]
}

【问题讨论】:

    标签: php json


    【解决方案1】:

    使用json_decode() 解码后,您会发现还有一个您没有考虑到的额外数组:

    $json = '{
        "prsp_sol": [
            {
                "proy_sim.name": "Vehículos",
                "prsp_def.name": "TRACTOR"
            }
        ]
    }';
    
    $decoded = json_decode($json, true); // true makes it an array
    print_r($decoded);
    
    echo $decoded['prsp_sol'][0]['proy_sim.name'];
    //-----------------------^ additional nested array
    

    输出:

    Array
    (
        [prsp_sol] => Array
            (
                [0] => Array
                    (
                        [proy_sim.name] => Vehículos
                        [prsp_def.name] => TRACTOR
                    )
            )
    )
    
    Vehículos
    

    这是example

    名称中的要点无关紧要。

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 1970-01-01
      • 2017-05-30
      • 2020-12-27
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      相关资源
      最近更新 更多