1

I have a client who's using the Yelp API to get the 10 closest results to a point using a latitude and longitude. The api query combines a "list" of categories together and then passes it to the query in Yelp.

Something like this:

 $categories = [ 'pharmacy', 'hospitals', 'firedepartments', 'policedepartments', 'parks', 'restaurants', 'grocery' ];

 $list = join($categories, ",");

When that call is made, Yelp doesn't seem to return all the category alias' correctly. But, when each category is called individually, it does return the category alias' right.

So when I use a foreach() statement to loop the categories, like this:

    foreach($categories as $k=>$v){

      $resulted = yelp_search($v, $range, $lat, $lon, $num);
      $resulted = json_decode($resulted, true);
      $result[] = array_merge($result, $resulted);
    }

It will add it to the $result array. But the problem is, that it will only add the last category to the list, for example in this case "grocery". So when the function in javascript is selected for "closest" it will only show the grocery icons on the map and in the list, not everything it returns.

That's issue #1, issue #2 is this. In the list displayed, because 'category' is being set as 'closest' to a function in javascript, I have to go through and look at the categories in the json data returned, and check if it matches something in the list.

like this

                    for (var i = 0; i < count; i++) {
                  var b = results[category]['businesses'][i];

                  if(category == "closest"){

                      ccount = b.categories.length;

                      for(var m = 0; m < ccount; m++){
                        console.log(b.categories[m]['alias']);

                        var alias = cats.includes(b.categories[m]['alias']);

                        if(alias){

                            var c = b.categories[m]['alias'];
                            break;

                        }
                        else{
                            var c = results[category]['category_name'];
                        }
                      }



                  update = update + '<tr><td role="button" class="td_menu" data-category="' + c + '" data-index="' + i + '"><img style="height: 20px; padding-right: 5px;" src="<?php echo get_template_directory_uri(); ?>/images/' + c + '.png"><span style="vertical-align: top;">' + b.name + 
                '</span></td><td></td><td>' + (b.distance * 0.00062137).toFixed(2) + '</td><td>mi</td></tr>\n';               

                  }
                  else{

                  update = update + '<tr><td role="button" class="td_menu" data-category="' + category + '" data-index="' + i + '"><img style="height: 20px; padding-right: 5px;" src="<?php echo get_template_directory_uri(); ?>/images/' + category + '.png"><span style="vertical-align: top;">' + b.name + 
                '</span></td><td></td><td>' + (b.distance * 0.00062137).toFixed(2) + '</td><td>mi</td></tr>\n';                   


                  }



                }

All of this part seems to work OK, because it does return the proper category alias (aka grocery) and the icons in the list do show. I suspect that once I can figure out how to get issue #1 resolved, issue #2 will fix itself.

I need some guidance on this please. I've spent 2 days trying to get this right and to show the proper icons in the list, as well as getting the closest data so that it does contain all the alias' in the data.

halfer
  • 18,701
  • 13
  • 79
  • 158
MrTechie
  • 1,645
  • 2
  • 19
  • 32

1 Answers1

0

The Yelp answer contains also metadata, you cannot just concatenate them : you want to merge the businesses only :

$result = array();
foreach($categories as $k=>$v){
  $resulted = yelp_search($v, $range, $lat, $lon, $num);
  $resulted = json_decode($resulted, true);

  if(array_key_exists('businesses', $result))
    $result['businesses'] = array_merge($result['businesses'], $resulted['businesses']);
  else
    $result = $resulted ;
}

Note that the metadata are wrong (the total for example), maybe you need to update that too if you need it. Yelp returns always the most specific keywords, so even with 'restaurants', you will have 'libanese' or 'pizza' and not 'restaurants'. If you need this keyword for displaying a marker, you can add it to the list if it is not already there.

Yelp API returns the distance from the location you are looking at : once you have your whole collection, you can sort your whole array per decreasing distance :

usort($result['businesses'], function ($a, $b){
    return $a['distance'] - $b['distance'] ;
});

Now you have a list of businesses of different categories, with the closest always in first position, whatever its category is.

Joffrey Schmitz
  • 2,141
  • 3
  • 16
  • 25