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

For loop keep printing the same information

我正在尝试打印 Zomato API 的数据。它打印第一个细节,然后打印第二个并不断重复第二个细节。我假设它不会在 1 之后增加 $i 的值,因此打印相同的细节。我做错了什么?

我的代码:

<?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>';
?>

你在底部的循环中 re-assigning $i。我的猜测是内部循环只有 2 个元素,因此它会在每个循环中将 $i 重新分配给 1,使您陷入无限循环。

将内部循环变量的名称从 $i 更改为类似 $j

变化:

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

收件人:

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