Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Postcodes.IO数组进行数据回调_Php_Arrays - Fatal编程技术网

Php 使用Postcodes.IO数组进行数据回调

Php 使用Postcodes.IO数组进行数据回调,php,arrays,Php,Arrays,我正在使用该网站使用下面的代码批量检查邮政编码,当我尝试使用foreach时,print\r工作正常,我收到一条错误消息500 任何人都知道我错在哪里: require 'Postcodes-IO-PHP.php'; $postcode = new Postcode(); $lookup = $postcode->bulkLookup(array("UB4 9LL","TW5 0LY","W6 8LL")); //print_r($lookup); // Works fine forea

我正在使用该网站使用下面的代码批量检查邮政编码,当我尝试使用foreach时,print\r工作正常,我收到一条错误消息500

任何人都知道我错在哪里:

require 'Postcodes-IO-PHP.php';
$postcode = new Postcode();
$lookup = $postcode->bulkLookup(array("UB4 9LL","TW5 0LY","W6 8LL"));
//print_r($lookup); // Works fine

foreach($lookup as $item):
      echo '<span>'.$item['postcode'].'</span>';
      echo '<span>'.$item['parliamentary_constituency'].'</span>';
      echo '<span>'.$item['admin_district'].';</span>';
endforeach;
编辑:示例输出:

Array
(
    [0] => stdClass Object
        (
            [query] => TW5 0LY
            [result] => stdClass Object
                (
                    [postcode] => TW5 0LY
                    [quality] => 1
                    [eastings] => 513138
                    [northings] => 177155
                    [country] => England
                    [nhs_ha] => London
                    [longitude] => -0.37209917055311
                    [latitude] => 51.482044755215
                    [parliamentary_constituency] => Feltham and Heston
                    [european_electoral_region] => London
                    [primary_care_trust] => Hounslow
                    [region] => London
                    [lsoa] => Hounslow 011D
                    [msoa] => Hounslow 011
                    [incode] => 0LY
                    [outcode] => TW5
                    [admin_district] => Hounslow
                    [parish] => Hounslow, unparished area
                    [admin_county] => 
                    [admin_ward] => Heston East
                    [ccg] => NHS Hounslow
                    [nuts] => Hounslow and Richmond upon Thames
                    [codes] => stdClass Object
                        (
                            [admin_district] => E09000018
                            [admin_county] => E99999999
                            [admin_ward] => E05000356
                            [parish] => E43000208
                            [ccg] => E38000084
                            [nuts] => UKI75
                        )

                )

        )

    [1] => stdClass Object
        (
            [query] => UB4 9LL
            [result] => stdClass Object
                (
                    [postcode] => UB4 9LL
                    [quality] => 1
                    [eastings] => 511267
                    [northings] => 182446
                    [country] => England
                    [nhs_ha] => London
                    [longitude] => -0.3973643481139
                    [latitude] => 51.529970749762
                    [parliamentary_constituency] => Hayes and Harlington
                    [european_electoral_region] => London
                    [primary_care_trust] => Hillingdon
                    [region] => London
                    [lsoa] => Hillingdon 020D
                    [msoa] => Hillingdon 020
                    [incode] => 9LL
                    [outcode] => UB4
                    [admin_district] => Hillingdon
                    [parish] => Hillingdon, unparished area
                    [admin_county] => 
                    [admin_ward] => Yeading
                    [ccg] => NHS Hillingdon
                    [nuts] => Harrow and Hillingdon
                    [codes] => stdClass Object
                        (
                            [admin_district] => E09000017
                            [admin_county] => E99999999
                            [admin_ward] => E05000344
                            [parish] => E43000207
                            [ccg] => E38000082
                            [nuts] => UKI74
                        )

                )

        )

    [2] => stdClass Object
        (
            [query] => W6 8LL
            [result] => stdClass Object
                (
                    [postcode] => W6 8LL
                    [quality] => 1
                    [eastings] => 523864
                    [northings] => 177965
                    [country] => England
                    [nhs_ha] => London
                    [longitude] => -0.21742782750815
                    [latitude] => 51.487079216243
                    [parliamentary_constituency] => Hammersmith
                    [european_electoral_region] => London
                    [primary_care_trust] => Hammersmith and Fulham
                    [region] => London
                    [lsoa] => Hammersmith and Fulham 014A
                    [msoa] => Hammersmith and Fulham 014
                    [incode] => 8LL
                    [outcode] => W6
                    [admin_district] => Hammersmith and Fulham
                    [parish] => Hammersmith and Fulham, unparished area
                    [admin_county] => 
                    [admin_ward] => Fulham Reach
                    [ccg] => NHS Hammersmith and Fulham
                    [nuts] => Kensington & Chelsea and Hammersmith & Fulham
                    [codes] => stdClass Object
                        (
                            [admin_district] => E09000013
                            [admin_county] => E99999999
                            [admin_ward] => E05000255
                            [parish] => E43000203
                            [ccg] => E38000070
                            [nuts] => UKI33
                        )

                )

        )

)

在阅读了
StdClass
之后,它变得更有意义了。我已经为可能遇到同样问题的其他人提供了一个完整的工作示例

此方法使用
textarea
输入发布邮政编码,每个邮政编码位于新行:

// Required postcode.io Class
require 'Postcodes-IO-PHP.php';

// Prepares the postcode array by removing new lines
$query = explode("\n", str_replace("\r", "", $_POST["postcodes"])); 

// Runs the postcode lookup and returns return
$postcode = new Postcode();
$lookup = $postcode->bulkLookup($query);

// Output results
foreach($lookup as $item):
    echo "Postcode: ".$item->query."<br>";
    echo "District: ".$item->result->admin_district."<br>";
    echo "Constituency: ".$item->result->parliamentary_constituency."<br>";
eachforeach

你能把错误的输出和打印($lookup)的值都包括进来吗?这个错误只是一个标准错误。Chrome:domain.com目前无法处理这个请求。我想你可能需要做
foreach($lookup->result as$item):
并使用
$item->postcode>而不是
$item['postcode']
etc,作为
json\u decode
返回对象,默认情况下不返回数组。如果希望它返回关联数组,则需要使用
json\u decode($result,true)
。对于单个邮政编码,“批量”查找是否有效?
// Required postcode.io Class
require 'Postcodes-IO-PHP.php';

// Prepares the postcode array by removing new lines
$query = explode("\n", str_replace("\r", "", $_POST["postcodes"])); 

// Runs the postcode lookup and returns return
$postcode = new Postcode();
$lookup = $postcode->bulkLookup($query);

// Output results
foreach($lookup as $item):
    echo "Postcode: ".$item->query."<br>";
    echo "District: ".$item->result->admin_district."<br>";
    echo "Constituency: ".$item->result->parliamentary_constituency."<br>";
eachforeach
<html>
<body>
    <form method="post">
        <textarea name="postcodes"></textarea>
        <input type="submit" value="Search Postcodes">
    </form>
</body>
</html>
Postcode: SW1A 1AA
District: Westminster
Constituency: Cities of London and Westminster