从 PHP JSON 数据中获取特定值

假设您有一个 JSON 数据结构,例如:

{
    'succeed': true,
    'adaptor': 'sldp',
    'folder': 'live02_nimble',
    'load_balance': {
        'host/name': 'hkg-edge-2.wecast.hk',
        'ip': '123.138.137.128'
    }
}

您可以使用以下 PHP 代码来获取 'hkg-edge-2.wecast.hk' 的值:

$json = '{  
    'succeed': true,  
    'adaptor': 'sldp',  
    'folder': 'live02_nimble',  
    'load_balance': {  
        'host/name': 'hkg-edge-2.wecast.hk',  
        'ip': '123.138.137.128'  
    }  
}';

$data = json_decode($json, true);
$host = $data['load_balance']['host/name'];
echo $host; // Output: hkg-edge-2.wecast.hk

这里使用了 json_decode 函数将 JSON 数据解码为 PHP 数组,然后通过数组索引获取 'hkg-edge-2.wecast.hk' 的值。

总结: 通过使用 json_decode 函数和数组索引,您可以轻松地在 PHP 中从 JSON 数据中获取特定值。

从 PHP JSON 数据中获取特定值 - 示例

原文地址: https://www.cveoy.top/t/topic/qm5f 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录