在curl GET请求后循环遍历多维数组

时间:2019-06-03 23:30:46

标签: php

在成功执行curl GET请求之后,当我的数据转储显示有10个结果时,我只能从请求中回显[0]索引。不确定我的循环在做什么错,以便可以显示所有10个结果。

foreach循环,for循环。

<!-- Search Results -->
 <?php
if(isset($_GET['submit'])){

    /** Get User Input From Form Fields **/
    $provider_first_name = $_GET['first_name'];
    $provider_last_name = $_GET['last_name'];
    $provider_state = $_GET['state'];
    $provider_postal_code = $_GET['postal_code'];

    /** Initialize curl/API Request **/
    $curl = curl_init();
    curl_setopt_array($curl, array(
                          CURLOPT_URL => "https://npiregistry.cms.hhs.gov/api?first_name=". $provider_first_name . "&last_name=" . $provider_last_name . "&state=" . $provider_state . "&postal_code=" . $provider_postal_code . "",
                          CURLOPT_RETURNTRANSFER => true,
                          CURLOPT_ENCODING => "",
                          CURLOPT_MAXREDIRS => 10,
                          CURLOPT_TIMEOUT => 30,
                          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                          CURLOPT_CUSTOMREQUEST => "GET",
                          ));

    /** Execute curl Response **/
    $response = curl_exec($curl);
    /** Assign Possible curl Errors **/
    //$err = curl_error($curl);
    /** Close the curl Request **/
    //curl_close($curl);
    /** If Error Echo Message **/
    //if ($err === true) {
    //echo "<div class='col-md-12 col-sm-12 flex-center flex-column'><div class='alert alert-danger'><i class='fa fa-hand-paper-o' aria-hidden='true'></i> Please Try Search Again: " . $err . "</div></div>"; 
    //} 

    /** Else, Decode JSON Response and Echo Search Results **/
    $json = json_decode($response, TRUE);
    //
    if($json['results'][0] === null || $json['results'][0] == FALSE || $json['results'][0] == ''){
        echo "<div class='col-md-12 col-sm-12 flex-center flex-column'><div class='alert alert-warning'><p class='small'><i class='fa fa-hand-paper-o' aria-hidden='true'></i> Please Try Searching Again. The Provider You Are Looking For Could Not Be Located. Alternatively, You Can Post Your Health Care Provider Details by Clicking Here.</p></div></div>";
    }else{
        //
        var_dump($json);
        foreach($json as $doc){
            /** JSON Responses Listed Below **/
            $p_name = $doc['results'][0]['basic']['first_name'] . " " . $doc['results'][0]['basic']['last_name'] . ", " . $doc['results'][0]['basic']['credential'];
            $p_npi = $doc['results'][0]['number'];
            $p_address_one = $doc['results'][0]['addresses'][0]['address_1'];
            $p_address_two = $doc['results'][0]['addresses'][0]['city'] . ", " . $doc['results'][0]['addresses'][0]['state'] . " " . $doc['results'][0]['addresses'][0]['postal_code'];
            $p_phone = $doc['results'][0]['addresses'][1]['telephone_number'];

            //** Echo Responses In Blue-Bordered Card **/
            echo "
                  <div class='col-md-4 col-sm-12 wow fadeIn' style='padding-top: 9px;'>
                      <div class='card border border-primary mb-3' style='max-width: 18rem;'>
                          <div class='card-header text-center'>
                              <i class='fa fa-user-md' aria-hidden='true'></i> ".$p_name."
                          </div>
                          <div class='card-body text-primary text-center'>
                              NPI Number: ".$p_npi."
                          </div>
                          <p class='card-text text-center'>
                              <i class='fa fa-map-marker' aria-hidden='true'></i> ".$p_address_one." 
                          </p>
                          <p class='card-text text-center'>
                              ".$p_address_two." 
                          </p>
                          <p class='card-text text-center'>
                              <i class='fa fa-phone cyan-text' aria-hidden='true'></i> ".$p_phone."
                          </p>
                          <form action='' method='post'>
                          <div class='flex-center'>
                              <button class='btn btn-sm btn-cyan' type='submit' name='post_doc'><i class='fa fa-plus-circle' aria-hidden='true'></i> Add Provider</button>
                          </div>
                          </form>
                      </div> 
                  </div>
                  ";
        }
    }
}
?>
<!-- /Search Results -->

var_dump($ json);显示正确,但是我的回声不起作用。

1 个答案:

答案 0 :(得分:0)

$json['results']是结果数组,因此您应该对其进行循环。在循环内,您无需索引['results'][0],只需使用$doc

        foreach($json['results'] as $doc){
            /** JSON Responses Listed Below **/
            $p_name = $doc['basic']['first_name'] . " " . $doc['basic']['last_name'] . ", " . $doc['basic']['credential'];
            $p_npi = $doc['number'];
            $p_address_one = $doc['addresses'][0]['address_1'];
            $p_address_two = $doc['addresses'][0]['city'] . ", " . $doc['addresses'][0]['state'] . " " . $doc['addresses'][0]['postal_code'];
            $p_phone = $doc['addresses'][1]['telephone_number'];