1

i am trying to get few strings from json but with no success yet :(

i tried many codes but still no luck and can't understand this fully

the json example is :

{
    "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

5
  • 1
    I don't see items in the JSON. Commented Nov 6, 2019 at 20:44
  • 1
    application is an array. You need to use ['application'][0] or iterate over it. Commented Nov 6, 2019 at 20:45
  • You want to get the keys bw_audio, bw_video, or you want to get the values of those keys? Commented Nov 6, 2019 at 20:46
  • the values of the keys .......... i am sorry i will update my question Commented Nov 6, 2019 at 20:50
  • I still don't see where $array['items'] is. I assume the JSON you showed is $array. Commented Nov 6, 2019 at 20:52

1 Answer 1

1

You have no items key in the JSON and application is an array so you should do:

$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].

Sign up to request clarification or add additional context in comments.

1 Comment

@chedlywalid You have no items key in the JSON. I edited my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.