如何提取此数组的一部分 - PHP


How to extract parts of this array - PHP

>我正在尝试打印这个数组的一部分,基本上所有带有"ProviderRedirect.ashx"的URL[href]基本上[0]到[无限]

Array
(
    [name] => HC Redirect
    [count] => 66
    [frequency] => Daily
    [version] => 14
    [newdata] => 1
    [lastrunstatus] => partial
    [thisversionstatus] => success
    [nextrun] => Sun Jan 17 2016 14:03:08 GMT+0000 (UTC)
    [thisversionrun] => Sat Jan 16 2016 14:03:08 GMT+0000 (UTC)
    [results] => Array
    (
        [collection1] => Array
            (
                [0] => Array
                    (
                        [Hotel Search] => Array
                            (
                                [href] => https://www.domain.com/ProviderRedirect.ashx?key=0.6359329.272723160.5179.GBP.1729297590&saving=410&source=32-0
                                [text] => View Deal
                            )
                        [index] => 1
                        [url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
                    )
                [1] => Array
                    (
                        [Hotel Search] => Array
                            (
                                [href] => https://www.domain.com/ProviderRedirect.ashx?key=0.21199849.272723130.457.GBP.753573779&source=32-0
                                [text] => View Deal
                            )
                        [index] => 2
                        [url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
                    )
                [2] => Array
                    (
                        [Hotel Search] => Array
                            (
                                [href] => https://www.domain.com/ProviderRedirect.ashx?key=0.23906211.272723157.1326.GBP.2008823249&source=32-0
                                [text] => View Deal
                            )
                        [index] => 3
                        [url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
                    )
                [3] => Array
                    (
                        [Hotel Search] => Array
                            (
                                [href] => https://www.domain.com/ProviderRedirect.ashx?key=0.5242811.272723157.3854.GBP.1642352834&source=32-0
                                [text] => View Deal
                            )
                        [index] => 4
                        [url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
                    )
                [4] => Array
                    (
                        [Hotel Search] => Array
                            (
                                [href] => https://www.domain.com/ProviderRedirect.ashx?key=0.675524.272723160.1457.GBP.2121712597&saving=18&source=32-0
                                [text] => View Deal
                            )
                        [index] => 5
                        [url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
                    )
                [5] => Array
                    (
                        [Hotel Search] => Array
                            (
                                [href] => https://www.domain.com/ProviderRedirect.ashx?key=0.5743724.272723155.847.GBP.1001086600&source=32-0
                                [text] => View Deal
                            )
                        [index] => 6
                        [url] => https://www.domain.com/Hotels/Search?destination=place:London&checkin=2016-09-02&checkout=2016-09-09&Rooms=1&adults_1=2&languageCode=EN¤cyCode=GBP&pageSize=50
                    )
                [6] => Array

要么使用array_walk_recursive()函数,要么编写自己的函数来递归遍历数组并打印所有 URL。

假设$arr是你的数组。

方法(1):

array_walk_recursive($arr, function($item, $key) {
    if ($key == "href") {
        echo $item . "<br />";
    }
});

方法(2):

function process_array($array){
    foreach($array as $key => $value){
        if(is_array($value)){
            process_array($value);
        }else{
            if($key == "href"){
                echo $value . "<br />";
            }
        }
    }
}
process_array($arr);
        $items = array(
            'results' => array(
                'collection1'=> array(
                    array(
                        'hotel_search'=>array(
                            'href'=>'value'
                        ),
                        'index'=>'value'
                    ),
                    array(
                        'hotel_search'=>array(
                            'href'=>'value'
                        ),
                        'index'=>'value'
                    )
                )
            )
        );
        $collection1 = $items['results']['collection1'];
        foreach($collection1 as $collection) {
             printf("%s", $collection['hotel_search']['href']);
        }

不过,您需要遍历结果数组以避免硬编码

如果您需要做的就是打印值,array_walk_recursive可以完成这项工作:

array_walk_recursive($array, function($item, $key) {
    if ('href' === $key) {
        echo "$item'n";
    }
});