Search code examples
phpapifor-loopzomato-api

For loop keep printing the same information


I am trying to print the data of 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. 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>';
?>

Solution

  • You're re-assigning $i within the loop at the bottom. 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.

    Change the name of the inner loop variable from $i to something like $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 '***************************************';
        }