简体   繁体   English

For 循环不断打印相同的信息

[英]For loop keep printing the same information

I am trying to print the data of Zomato API.我正在尝试打印 Zomato API 的数据。 It prints the first detail, then second and keep repeating the second detail.它打印第一个细节,然后打印第二个并不断重复第二个细节。 I am assuming its not increasing the value of $i after 1 hence printing the same details.我假设它不会在 1 之后增加$i的值,因此打印相同的细节。 What am I doing wrong?我究竟做错了什么?

My code:我的代码:

<?php
// Errors on
error_reporting(E_ALL);
?>

<h1>Testing of Zomato API Code</h1>

<?php
function perform_api_call($param)
{
    // Get cURL resource
    $curl = curl_init();
    // Curl options
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => ['Accept: application/json',
        'user-key:' . '*******'],
        CURLOPT_URL => 'https://developers.zomato.com/api/v2.1/' . $param,
    ));
    // Send the request & save response to $resp
    $resp = curl_exec($curl);
    // Check for errors if curl_exec fails
    if (!curl_exec($curl))
    {
        die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
    }
    // Close request to clear up some resources
    curl_close($curl);
    // Decode json
    return json_decode($resp, true);
}

echo '<h2>Perform the search</h2>';
$api_call_string = 'search?entity_type=city&q=Brisbane';
echo 'API Call STRING ' . $api_call_string . '<br>';
$search = perform_api_call($api_call_string);
$res_high = array();

/*
echo '<pre>';
echo 'LINE: ' . __LINE__ . '<br>';
var_dump($search);
echo '</pre>';

*/
for ($i = 0;$i < count($search['restaurants']);$i++)
{
    echo count($search['restaurants']);
    echo 'testig';
    echo $i;
    echo '***************************************';
    echo '<h2>Get the Restaurant ID</h2>';
    $res_id = $search['restaurants'][$i]['restaurant']['R']['res_id'];
    $res_name = $search['restaurants'][$i]['restaurant']['name'];

    $res_link = $search['restaurants'][$i]['restaurant']['url'];
    $res_phoneNo = $search['restaurants'][$i]['restaurant']['phone_numbers'];
    $res_userRating = $search['restaurants'][$i]['restaurant']['user_rating']['aggregate_rating'];
    $res_cusines = $search['restaurants'][$i]['restaurant']['cuisines'];
    $res_photos = $search['restaurants'][$i]['restaurant']['photos_url'];
    $res_priceRange = $search['restaurants'][$i]['restaurant']['price_range'];
    $res_currency = $search['restaurants'][$i]['restaurant']['currency'];

    $res_menu = $search['restaurants'][$i]['restaurant']['menu_url'];
    $res_averageCostForTwo = $search['restaurants'][$i]['restaurant']['average_cost_for_two'];
    $res_establishment = $search['restaurants'][$i]['restaurant']['establishment'][0];

    $res_address = $search['restaurants'][$i]['restaurant']['location']['address'];
    $countForHighlights = count($search['restaurants'][$i]['restaurant']['highlights']);


         for ($y=0; $y<$countForHighlights;$y++) {
        $res_highlights = $search['restaurants'][$i]['restaurant']['highlights'][$y];
         }

  

    echo 'Res ID = ' . $res_id;
    echo '<br>';
    echo 'Res name = ' . $res_name;
    echo '<br>';

    echo 'Res link = ' . $res_link;
    echo '<br>';
    echo 'Res phoneno = ' . $res_phoneNo;
    echo '<br>';
    echo 'Res userrating = ' . $res_userRating;
    echo '<br>';
    echo 'Res cusines = ' . $res_cusines;
    echo '<br>';
    echo 'Res menu = ' . $res_menu;
    echo '<br>';
    echo 'Res photos = ' . $res_photos;
    echo '<br>';
    echo 'Res price range = ' . $res_priceRange;
    echo '<br>';
    echo 'Res currency = ' . $res_currency;
    echo '<br>';
    echo 'Res establishment = ' . $res_establishment;
    echo '<br>';
    echo 'Res average cost for two = ' . $res_averageCostForTwo;
    echo '<br>';
    echo 'Res address = ' . $res_address;
    echo '<br>';
    foreach ($res_high as $result)
    {
        echo $result, '<br>';
    }
    echo '*********************************************************************';
    $res_high = array();
    $res_name = "";
/*
    echo '<h2>Get the Reviews</h2>';
    $api_call_string = 'reviews?res_id=' . $res_id;
    echo 'API Call STRING ' . $api_call_string . '<br>';
    $reviews = perform_api_call($api_call_string);
    */
    for ($i = 0;$i < count($reviews['user_reviews']);$i++)
    {
        $userreview = $reviews['user_reviews'][$i]['review']['review_text'];
        echo 'user review = ' . $userreview;
        echo '***************************************';
    }
}

echo '<pre>';
echo 'LINE: ' . __LINE__ . '<br>';
var_dump($reviews);
echo '</pre>';
?>

You're re-assigning $i within the loop at the bottom.您正在底部的循环中重新分配 $i 。 My guess is the inner loop only has 2 elements so it keep reassigning $i to 1 every loop putting you in an indefinite loop.我的猜测是内部循环只有 2 个元素,因此每次循环都会将 $i 重新分配给 1,从而使您陷入无限循环。

Change the name of the inner loop variable from $i to something like $j将内部循环变量的名称从 $i 更改为 $j

Change:改变:

for ($i = 0;$i < count($reviews['user_reviews']);$i++)
    {
        $userreview = $reviews['user_reviews'][$i]['review']['review_text'];
        echo 'user review = ' . $userreview;
        echo '***************************************';
    }

To:到:

for ($j = 0;$j < count($reviews['user_reviews']);$j++)
    {
        $userreview = $reviews['user_reviews'][$j]['review']['review_text'];
        echo 'user review = ' . $userreview;
        echo '***************************************';
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM