如何提取这个数组的部分 - PHP

时间:2016-01-16 14:11:47

标签: php

我正在尝试打印这个数组的一部分,基本上所有的URL [href]都带有“ProviderRedirect.ashx”,基本上是[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

3 个答案:

答案 0 :(得分:0)

        $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']);
        }

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

答案 1 :(得分:0)

如果您只需要打印值,则array_walk_recursive可以完成这项工作:

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

答案 2 :(得分:0)

使用array_walk_recursive()函数或编写自己的函数来递归遍历数组并打印所有网址。

假设$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);