在多维数组中按键=值搜索,其中搜索条件包含正则表达式

时间:2015-03-16 15:48:26

标签: php arrays

Array
(
    [display_id] => 8sPj0Ic8KQ8
    [formats] => Array
        (
            [0] => Array
                (
                    [http_headers] => Array
                        (
                            [Accept-Charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
                            [Accept-Language] => en-us,en;q=0.5
                            [Accept-Encoding] => gzip, deflate
                            [Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
                            [User-Agent] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
                        )

                    [format] => nondash-171 - audio only (DASH audio)
                    [url] => https://r9---sn-a5m7lnel.googlevideo.com/videoplayback?keepalive=yes&itag=171&requiressl=yes&signature=7759397C37EC0668E5FE39474B009809F26CBB13.42BE0F5C6E5BA6D7B6C1F4E39AB494B47E24D3FF&mm=31&gir=yes&dur=210.862&id=o-AH0zuSgN411QzK7H-qW3BTqHY_Rn09uGX4CG-byG82HF&sver=3&fexp=900504%2C900720%2C905648%2C907263%2C917000%2C927622%2C934954%2C937420%2C9406090%2C9406556%2C9406978%2C9407103%2C9407415%2C943917%2C945094%2C948124%2C951511%2C951703%2C952302%2C952612%2C952901%2C955301%2C957201%2C959701&clen=3196194&source=youtube&mv=m&mt=1426520286&ms=au&expire=1426541958&lmt=1411477600106977&mime=audio%2Fwebm&upn=0AAhyEqh4fo&gcr=us&sparams=clen%2Cdur%2Cgcr%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cms%2Cmv%2Cpl%2Crequiressl%2Csource%2Cupn%2Cexpire&key=yt5&pl=21&initcwndbps=1905000&ipbits=0&ip=209.95.42.9&ratebypass=yes
                    [vcodec] => none
                    [format_note] => DASH audio
                    [abr] => 128
                    [player_url] => 
                    [ext] => webm
                    [preference] => -10050
                    [format_id] => nondash-171
                )

            [1] => Array
                (
                    [http_headers] => Array
                        (
                            [Accept-Charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
                            [Accept-Language] => en-us,en;q=0.5
                            [Accept-Encoding] => gzip, deflate
                            [Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
                            [User-Agent] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
                        )

                    [container] => m4a_dash
                    [format] => nondash-140 - audio only (DASH audio)
                    [url] => https://r9---sn-a5m7lnel.googlevideo.com/videoplayback?keepalive=yes&itag=140&requiressl=yes&signature=7C31A2F3665FE0A83A61176A4CA8E5043D88FE04.3D7E936F3B96AFBB02F0D938C4D497A9D3BACE9A&mm=31&gir=yes&dur=210.930&id=o-AH0zuSgN411QzK7H-qW3BTqHY_Rn09uGX4CG-byG82HF&sver=3&fexp=900504%2C900720%2C905648%2C907263%2C917000%2C927622%2C934954%2C937420%2C9406090%2C9406556%2C9406978%2C9407103%2C9407415%2C943917%2C945094%2C948124%2C951511%2C951703%2C952302%2C952612%2C952901%2C955301%2C957201%2C959701&clen=3386965&source=youtube&mv=m&mt=1426520286&ms=au&expire=1426541958&lmt=1417194435725180&mime=audio%2Fmp4&upn=0AAhyEqh4fo&gcr=us&sparams=clen%2Cdur%2Cgcr%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cms%2Cmv%2Cpl%2Crequiressl%2Csource%2Cupn%2Cexpire&key=yt5&pl=21&initcwndbps=1905000&ipbits=0&ip=209.95.42.9&ratebypass=yes
                    [vcodec] => none
                    [format_note] => DASH audio
                    [abr] => 128
                    [player_url] => 
                    [ext] => m4a
                    [preference] => -10050
                    [format_id] => nondash-140
                    [acodec] => aac
                )

我希望获得特定格式的[url],但我需要匹配代码

例如,假设我想获得url171的{​​{1}}。在这种情况下,输出应为format_id

1 个答案:

答案 0 :(得分:0)

非常简单。只需循环并检查:

foreach($array['formats'] as $value) {
    if(strpos($value['format_id'], 171) !== false) {
        $url = $value['url'];
        break;
    }
}

echo $url;