0

I'm sure I must be missing something obvious but I am trying to access some venue and category information from the below data and retrieve it using PHP.

When I include ['venues'] next to ['response'] in my foreach statement, I can access the name, latitude and longitude easily, no issues. However, I need to access some information from ['categories'] as well, so I tried moving ['venues'] out of the foreach statement and onto each line that accesses that section of the array. This then causes an error that each instance of ['venues'] and ['categories'] is an unidentified index

What am I missing?

Data I'm using:

data: {
    meta: {
        code: 200,
        requestId: "61044a3db649b50f959a1dd9"
    },
    response: {
        venues: [
            {
                id: "4ac518cdf964a520e6a520e3",
                name: "National Gallery",
                location: {
                    address: "Trafalgar Sq",
                    lat: 51.50887601013219,
                    lng: -0.1284778118133545,
                    labeledLatLngs: [
                        {
                            label: "display",
                            lat: 51.50887601013219,
                            lng: -0.1284778118133545
                        }
                    ],
                    postalCode: "WC2N 5DN",
                    cc: "GB",
                    city: "London",
                    state: "Greater London",
                    country: "United Kingdom",
                    formattedAddress: [
                        "Trafalgar Sq",
                        "London",
                        "Greater London",
                        "WC2N 5DN",
                        "United Kingdom"
                    ]
                },
                categories: [
                    {
                        id: "4bf58dd8d48988d18f941735",
                        name: "Art Museum",
                        pluralName: "Art Museums",
                        shortName: "Art Museum",
                        icon: {
                            prefix: "https://ss3.4sqi.net/img/categories_v2/arts_entertainment/museum_art_",
                            suffix: ".png"
                        },
                        primary: true
                    }
                ], etc

My PHP:

?php

$url = "https://api.foursquare.com/v2/venues/search?client_id=foo&client_secret=bar&v=20210703&near=" . $_REQUEST['city'] . "," . $_REQUEST['country'] . "&categoryId=" . $_REQUEST['category'];

$curl = curl_init();

$ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
   
    $result=curl_exec($ch);

    curl_close($ch);

    $decode = json_decode($result,true);

    $countryData = [];

    foreach($decode['response'] as $result){
        $countryinfo = [];
        $countryinfo['name'] = $result['venues']['name'];
        $countryinfo['lat'] = $result['venues']['location']['lat'];
        $countryinfo['lng'] = $result['venues']['location']['lng'];
        $countryinfo['icon'] = $result['categories']['icon']['prefix'];
        $countryinfo['iconSuffix'] = $result['categories']['icon']['suffix'];

         array_push($countryData, $countryinfo);
    }

 
    $output['status']['code'] = "200";
    $output['status']['name'] = "ok";
    $output['status']['description'] = "success";
    $output['data'] = $countryData;
    
    header('Content-Type: application/json; charset=UTF-8');

    echo json_encode($output); 


?> 


If I include venues in the foreach statement, I get the desired results for name, lat and lng - so I'm not sure why taking it out and placing it on each line that accesses the venues array doesn't work?

PHP:

foreach($decode['response']['venues'] as $result){
        $countryinfo = [];
        $countryinfo['name'] = $result['name'];
        $countryinfo['lat'] = $result['location']['lat'];
        $countryinfo['lng'] = $result['location']['lng'];
        //$countryinfo['icon'] = $result['categories']['icon']['prefix'];
        //$countryinfo['iconSuffix'] = $result['categories']['icon']['suffix'];

         array_push($countryData, $countryinfo);
    }

Returns:

{
status: {
code: "200",
name: "ok",
description: "success"
},
data: [
{
name: "National Gallery",
lat: 51.50887601013219,
lng: -0.1284778118133545
},
{
name: "Elizabeth Tower (Big Ben) (Big Ben (Elizabeth Tower))",
lat: 51.50070194764357,
lng: -0.12458056211471556
},
``` etc
Sophie
  • 35
  • 1
  • 7
  • Your `venues` code can't be working correctly. `venues` is an array, you need to index it. – Barmar Jul 30 '21 at 19:45
  • You need to loop over the `venues` array, and for each venue you can get `$venue['location']` and `$venue['categories']` – Barmar Jul 30 '21 at 19:48
  • When I include ['venues'] in the foreach loop the code works fine for the name, lat and lng – Sophie Jul 31 '21 at 08:44
  • I get the below results: ` status: { code: "200", name: "ok", description: "success" }, data: [ { name: "National Gallery", lat: 51.50887601013219, lng: -0.1284778118133545 }, { name: "Elizabeth Tower (Big Ben) (Big Ben (Elizabeth Tower))", lat: 51.50070194764357, lng: -0.12458056211471556 },` etc – Sophie Jul 31 '21 at 08:46
  • If you have more to say, put it in the question. – Barmar Jul 31 '21 at 08:47

0 Answers0