简体   繁体   English

从 php 中的多级 json 获取字符串

[英]get string from multi level json in php

i am trying to get few strings from json but with no success yet:(我正在尝试从 json 获取一些字符串,但还没有成功:(

i tried many codes but still no luck and can't understand this fully我尝试了很多代码,但仍然没有运气,无法完全理解

the json example is: json 示例是:

{
    "nginx_version": "1.7.11.3 Gryphon",
    "nginx_rtmp_version": "1.1.4",
    "built": "Mar 19 2015 20:36:41",
    "pid": "4680",
    "uptime": "1876",
    "naccepted": "3",
    "bw_in": "404904",
    "bytes_in": "83699680",
    "bw_out": "404912",
    "bytes_out": "83700478",
    "server": {
        "application": [{
            "name": "msdk",
            "live": {
                "stream": {
                    "name": "xkiz531",
                    "time": "1854519",
                    "bw_in": "399752",
                    "bytes_in": "82628640",
                    "bw_out": "399752",
                    "bytes_out": "82628588",
                    "bw_audio": "178480",
                    "bw_video": "221272",
                    "client": [{
                        "id": "2788",
                        "address": "example.com\/live\/live",
                        "time": "1854519",
                        "flashver": "ngx-local-relay",
                        "dropped": "0",
                        "avsync": "-14",
                        "timestamp": "1853566",
                        "active": []
                    }, {
                        "id": "2787",
                        "address": "197.14.103.17",
                        "time": "1854987",
                        "flashver": "FMLE\/3.0 (compatible; FMSc\/1.0)",
                        "swfurl": "rtmp:\/\/example.com\/msdk",
                        "dropped": "0",
                        "avsync": "-14",
                        "timestamp": "1853566",
                        "publishing": [],
                        "active": []
                    }],
                    "meta": {
                        "video": {
                            "width": "1280",
                            "height": "720",
                            "frame_rate": "30",
                            "codec": "H264",
                            "profile": "High",
                            "compat": "0",
                            "level": "3.1"
                        },
                        "audio": {
                            "codec": "AAC",
                            "profile": "LC",
                            "channels": "2",
                            "sample_rate": "44100"
                        }
                    },
                    "nclients": "2",
                    "publishing": [],
                    "active": []
                },
                "nclients": "2"
            }
        }, {
            "name": "test",
            "live": {
                "nclients": "0"
            }
        }]
    }
}

the code i have now is:我现在的代码是:

$array = json_decode($json,TRUE);

var_dump($array);
foreach($array['items'] as $item) {
    echo $item['server']['application']['live']['stream']['name'];
}

i want to obtain the values of these keys on strings:我想在字符串上获取这些键的值:

bw_audio, bw_video, width, height, frame_rate, codec, profile, audio codec & sample rate bw_audio、bw_video、宽度、高度、帧速率、编解码器、配置文件、音频编解码器和采样率

You have no items key in the JSON and application is an array so you should do:您在 JSON 中没有items键,并且application是一个数组,因此您应该这样做:

$array = json_decode($json,TRUE);
echo $array['server']['application'][0]['live']['stream']['name'];

Note: if there may be more than one application, iterate foreach over the array instead of using [0] .注意:如果可能有多个应用程序,请在数组上迭代foreach而不是使用[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM