Jump to content

randomizing (shuffle)


Jeisson

Recommended Posts

Hi

I try to show lists in random order. I have json that I decode to arrays and loop over it and show the result.

Now I have teried to how it in a random order. but no result. tried allready several different ways..

here is something about:

$json_results = json_decode($results,true);
$groups = $json_results['response']['groups'];
echo '<ul>';
shuffle($groups);
    foreach($groups as $group)

It does continue to function, but with no randomizing order. just same as always..

please help.

Thanks :)

Link to comment
Share on other sites

Post the json data (results) so we can see what you are working with.

here is some json if it helps. it continies like this.

{
    "meta": {
        "code": 200,
        "requestId": "561d5921498ed5a1feee2f41"
    },
    "response": {
        "venues": [
            {
                "id": "516094748302afa746c58951",
                "name": "Five Guys",
                "contact": {
                    "phone": "+442072402057",
                    "formattedPhone": "+44 20 7240 2057",
                    "twitter": "five_guys",
                    "facebook": "19836964440",
                    "facebookUsername": "fiveguys",
                    "facebookName": "Five Guys"
                },
                "location": {
                    "address": "1-3 Long Acre",
                    "crossStreet": "at Cranbourne St",
                    "lat": 51.51177397755055,
                    "lng": -0.12685775756835938,
                    "postalCode": "WC2E 9LH",
                    "cc": "GB",
                    "city": "Covent Garden",
                    "state": "Greater London",
                    "country": "United Kingdom",
                    "formattedAddress": [
                        "1-3 Long Acre (at Cranbourne St)",
                        "Covent Garden",
                        "Greater London",
                        "WC2E 9LH",
                        "United Kingdom"
                    ]
                },
                "categories": [
                    {
                        "id": "4bf58dd8d48988d16c941735",
                        "name": "Burger Joint",
                        "pluralName": "Burger Joints",
                        "shortName": "Burgers",
                        "icon": {
                            "prefix": "https:\/\/ss3.4sqi.net\/img\/categories_v2\/food\/burger_",
                            "suffix": ".png"
                        },
                        "primary": true
                    }
                ],
                "verified": true,
                "stats": {
                    "checkinsCount": 14621,
                    "usersCount": 9895,
                    "tipCount": 348
                },
                "url": "http:\/\/www.fiveguys.co.uk",
                "allowMenuUrlEdit": true,
                "specials": {
                    "count": 0,
                    "items": [
                        
                    ]
                },
                "hereNow": {
                    "count": 3,
                    "summary": "3 people are here",
                    "groups": [
                        {
                            "type": "others",
                            "name": "Other people here",
                            "count": 3,
                            "items": [
                                
                            ]
                        }
                    ]
                },
                "storeId": "",
                "referralId": "v-1444763937"
            },
            {
                "id": "4cf9619bee9cb60c7bc883ad",
                "name": "Hakkasan",
                "contact": {
                    "phone": "+442079277000",
                    "formattedPhone": "+44 20 7927 7000",
                    "twitter": "hakkasanmayfair"
                },
                "location": {
                    "address": "17 Bruton St",
                    "lat": 51.5104785955435,
                    "lng": -0.14460325241088867,
                    "postalCode": "W1J 6QB",
                    "cc": "GB",
                    "city": "Mayfair",
                    "state": "Greater London",
                    "country": "United Kingdom",
                    "formattedAddress": [
                        "17 Bruton St",
                        "Mayfair",
                        "Greater London",
                        "W1J 6QB",
                        "United Kingdom"
                    ]
                },
                "categories": [
                    {
                        "id": "4bf58dd8d48988d142941735",
                        "name": "Asian Restaurant",
                        "pluralName": "Asian Restaurants",
                        "shortName": "Asian",
                        "icon": {
                            "prefix": "https:\/\/ss3.4sqi.net\/img\/categories_v2\/food\/asian_",
                            "suffix": ".png"
                        },
                        "primary": true
                    }
                ],
                "verified": false,
                "stats": {
                    "checkinsCount": 7417,
                    "usersCount": 5145,
                    "tipCount": 255
                },
                "url": "http:\/\/hakkasan.com\/mayfair",
                "allowMenuUrlEdit": true,
                "specials": {
                    "count": 0,
                    "items": [
                        
                    ]
                },
                "hereNow": {
                    "count": 2,
                    "summary": "2 people are here",
                    "groups": [
                        {
                            "type": "others",
                            "name": "Other people here",
                            "count": 2,
                            "items": [
                                
                            ]
                        }
                    ]
                },
                "referralId": "v-1444763937"
            },
            {
                "id": "50509c8be4b0b28cb0bd0785",
                "name": "Mezzah Lounge",
                "contact": {
                    "phone": "+442077301234",
                    "formattedPhone": "+44 20 7730 1234",
                    "twitter": "harrods"
                },
                "location": {
                    "address": "87\u2013135 Brompton Rd",
                    "crossStreet": "Fourth Floor, Harrods",
                    "lat": 51.49864750188004,
                    "lng": -0.1628856295752017,
                    "postalCode": "SW1X 7XL",
                    "cc": "GB",
                    "city": "Greater London",
                    "state": "Greater London",
                    "country": "United Kingdom",
                    "formattedAddress": [
                        "87\u2013135 Brompton Rd (Fourth Floor, Harrods)",
                        "Greater London",
                        "Greater London",
                        "SW1X 7XL",
                        "United Kingdom"
                    ]
                }
            }
        ]
    }
}
Link to comment
Share on other sites

Are you sure that json is correct - gives my a syntax error when attempting to decode?

It's truncated. This is valid:

{
    "meta": {
        "code": 200,
        "requestId": "561d5921498ed5a1feee2f41"
    },
    "response": {
        "venues": [
            {
                "id": "516094748302afa746c58951",
                "name": "Five Guys",
                "contact": {
                    "phone": "+442072402057",
                    "formattedPhone": "+44 20 7240 2057",
                    "twitter": "five_guys",
                    "facebook": "19836964440",
                    "facebookUsername": "fiveguys",
                    "facebookName": "Five Guys"
                },
                "location": {
                    "address": "1-3 Long Acre",
                    "crossStreet": "at Cranbourne St",
                    "lat": 51.51177397755055,
                    "lng": -0.12685775756835938,
                    "postalCode": "WC2E 9LH",
                    "cc": "GB",
                    "city": "Covent Garden",
                    "state": "Greater London",
                    "country": "United Kingdom",
                    "formattedAddress": [
                        "1-3 Long Acre (at Cranbourne St)",
                        "Covent Garden",
                        "Greater London",
                        "WC2E 9LH",
                        "United Kingdom"
                    ]
                },
                "categories": [
                    {
                        "id": "4bf58dd8d48988d16c941735",
                        "name": "Burger Joint",
                        "pluralName": "Burger Joints",
                        "shortName": "Burgers",
                        "icon": {
                            "prefix": "https:\/\/ss3.4sqi.net\/img\/categories_v2\/food\/burger_",
                            "suffix": ".png"
                        },
                        "primary": true
                    }
                ],
                "verified": true,
                "stats": {
                    "checkinsCount": 14621,
                    "usersCount": 9895,
                    "tipCount": 348
                },
                "url": "http:\/\/www.fiveguys.co.uk",
                "allowMenuUrlEdit": true,
                "specials": {
                    "count": 0,
                    "items": [
                        
                    ]
                },
                "hereNow": {
                    "count": 3,
                    "summary": "3 people are here",
                    "groups": [
                        {
                            "type": "others",
                            "name": "Other people here",
                            "count": 3,
                            "items": [
                                
                            ]
                        }
                    ]
                },
                "storeId": "",
                "referralId": "v-1444763937"
            },
            {
                "id": "4cf9619bee9cb60c7bc883ad",
                "name": "Hakkasan",
                "contact": {
                    "phone": "+442079277000",
                    "formattedPhone": "+44 20 7927 7000",
                    "twitter": "hakkasanmayfair"
                },
                "location": {
                    "address": "17 Bruton St",
                    "lat": 51.5104785955435,
                    "lng": -0.14460325241088867,
                    "postalCode": "W1J 6QB",
                    "cc": "GB",
                    "city": "Mayfair",
                    "state": "Greater London",
                    "country": "United Kingdom",
                    "formattedAddress": [
                        "17 Bruton St",
                        "Mayfair",
                        "Greater London",
                        "W1J 6QB",
                        "United Kingdom"
                    ]
                },
                "categories": [
                    {
                        "id": "4bf58dd8d48988d142941735",
                        "name": "Asian Restaurant",
                        "pluralName": "Asian Restaurants",
                        "shortName": "Asian",
                        "icon": {
                            "prefix": "https:\/\/ss3.4sqi.net\/img\/categories_v2\/food\/asian_",
                            "suffix": ".png"
                        },
                        "primary": true
                    }
                ],
                "verified": false,
                "stats": {
                    "checkinsCount": 7417,
                    "usersCount": 5145,
                    "tipCount": 255
                },
                "url": "http:\/\/hakkasan.com\/mayfair",
                "allowMenuUrlEdit": true,
                "specials": {
                    "count": 0,
                    "items": [
                        
                    ]
                },
                "hereNow": {
                    "count": 2,
                    "summary": "2 people are here",
                    "groups": [
                        {
                            "type": "others",
                            "name": "Other people here",
                            "count": 2,
                            "items": [
                                
                            ]
                        }
                    ]
                },
                "referralId": "v-1444763937"
            },
            {
                "id": "50509c8be4b0b28cb0bd0785",
                "name": "Mezzah Lounge",
                "contact": {
                    "phone": "+442077301234",
                    "formattedPhone": "+44 20 7730 1234",
                    "twitter": "harrods"
                },
                "location": {
                    "address": "87\u2013135 Brompton Rd",
                    "crossStreet": "Fourth Floor, Harrods",
                    "lat": 51.49864750188004,
                    "lng": -0.1628856295752017,
                    "postalCode": "SW1X 7XL",
                    "cc": "GB",
                    "city": "Greater London",
                    "state": "Greater London",
                    "country": "United Kingdom",
                    "formattedAddress": [
                        "87\u2013135 Brompton Rd (Fourth Floor, Harrods)",
                        "Greater London",
                        "Greater London",
                        "SW1X 7XL",
                        "United Kingdom"
                    ]
                }
            }
        ]
    }
}
But, it's missing the response.groups part.

 

EDIT:

How do I do that?

shuffle($results)

gives error

What error are you getting?

Edited by scootstah
Link to comment
Share on other sites

Sorry guys. I have too many tabs open. Here is correct json. It is truncated because it is very long.

{"meta":{"code":200,"requestId":"561d68a0498ed5ffa3210d59"},"response":{"suggestedFilters":{"header":"Tap to show:","filters":[{"name":"Open now","key":"openNow"},{"name":"With specials","key":"specials"}]},"geocode":{"what":"","where":"london","center":{"lat":51.50853,"lng":-0.12574},"displayString":"London, Greater London, United Kingdom","cc":"GB","geometry":{"bounds":{"ne":{"lat":51.691643999655895,"lng":0.33418999705203406},"sw":{"lat":51.28467404417054,"lng":-0.5085579279369435}}},"slug":"london","longId":"72057594040571679"},"headerLocation":"London","headerFullLocation":"London","headerLocationGranularity":"city","query":"food","totalResults":246,"suggestedBounds":{"ne":{"lat":51.51263103703768,"lng":-0.12231439480184818},"sw":{"lat":51.50654795865553,"lng":-0.1314193179278593}},"groups":[{"type":"Recommended Places","name":"recommended","items":[{"reasons":{"count":0,"items":[{"summary":"This spot is popular","type":"general","reasonName":"globalInteractionReason"}]},"venue":{"id":"4ac518c4f964a5209aa420e3","name":"Wahaca","contact":{"phone":"+442072401883","formattedPhone":"+44 20 7240 1883","twitter":"wahaca"},"location":{"address":"66 Chandos Pl","lat":51.51022652594927,"lng":-0.12449473142623901,"postalCode":"WC2N 4HG","cc":"GB","neighborhood":"Covent","city":"London","state":"Greater London","country":"United Kingdom","formattedAddress":["66 Chandos Pl","London","Greater London","WC2N 4HG","United Kingdom"]},"categories":[{"id":"4bf58dd8d48988d1c1941735","name":"Mexican Restaurant","pluralName":"Mexican Restaurants","shortName":"Mexican","icon":{"prefix":"https:\/\/ss3.4sqi.net\/img\/categories_v2\/food\/mexican_","suffix":".png"},"primary":true}],"verified":true,"stats":{"checkinsCount":5406,"usersCount":4132,"tipCount":177},"url":"http:\/\/www.wahaca.co.uk","price":{"tier":2,"message":"Moderate","currency":"£"},"hasMenu":true,"rating":9.1,"ratingColor":"00B551","ratingSignals":534,"menu":{"type":"Menu","label":"Menu","anchor":"View Menu","url":"https:\/\/foursquare.com\/v\/wahaca\/4ac518c4f964a5209aa420e3\/menu","mobileUrl":"https:\/\/foursquare.com\/v\/4ac518c4f964a5209aa420e3\/device_menu"},"allowMenuUrlEdit":true,"hours":{"status":"Open until 11:00 PM","isOpen":true},"specials":{"count":0,"items":[]},"photos":{"count":220,"groups":[]},"hereNow":{"count":0,"summary":"Nobody here","groups":[]}},"tips":[{"id":"51decb2b498ee8a050cbd86b","createdAt":1373555499,"text":"Baharata düşkün Meksika Mutfağını seviyorsanız mutlaka uğrayın lezzetli ve uygun fiyatlı .","type":"user","canonicalUrl":"https:\/\/foursquare.com\/item\/51decb2b498ee8a050cbd86b","photo":{"id":"51decb2d50193b64eed0a5d3","createdAt":1373555501,"source":{"name":"Foursquare Web","url":"https:\/\/foursquare.com"},"prefix":"https:\/\/irs0.4sqi.net\/img\/general\/","suffix":"\/25407761_lcYmk9RXyoRZG0M8_1_DuW0oYSpCJy7CV_Iad-z78qs.jpg","width":1600,"height":1200},"photourl":"https:\/\/irs0.4sqi.net\/img\/general\/original\/25407761_lcYmk9RXyoRZG0M8_1_DuW0oYSpCJy7CV_Iad-z78qs.jpg","likes":{"count":32,"groups":[],"summary":"32 likes"},"logView":true,"todo":{"count":1},"user":{"id":"25407761","firstName":"Tugba","lastName":"Söyler","gender":"female","photo":{"prefix":"https:\/\/irs2.4sqi.net\/img\/user\/","suffix":"\/25407761-LOAJ2FBBDEDFAOPG.jpg"}}}],"referralId":"e-3-4ac518c4f964a5209aa420e3-0"},{"reasons":{"count":0,"items":[{"summary":"This spot is popular","type":"general","reasonName":"globalInteractionReason"}]},"venue":{"id":"53bab96d498e7e355fb53d6c","name":"Barrafina","contact":{"phone":"+442074401456","formattedPhone":"+44 20 7440 1456","twitter":"barrafinaadst","facebook":"705351912878392","facebookUsername":"BarrafinaAdelaideStreet","facebookName":"Barrafina Adelaide Street"},"location":{"address":"10 Adelaide St","crossStreet":"William IV St","lat":51.50945052890648,"lng":-0.1259482415312226,"postalCode":"WC2N 4HZ","cc":"GB","city":"London","state":"Greater London","country":"United Kingdom","formattedAddress":["10 Adelaide St (William IV St)","London","Greater London","WC2N 4HZ","United Kingdom"]},"categories":

 

it is foursquare api with explore as parameter. the first json was with an another parameter.

https://developer.foursquare.com/docs/venues/explore

 

 

shuffle($results) is error because results is not array

"Warning: shuffle() expects parameter 1 to be array, string given"

Edited by Jeisson
Link to comment
Share on other sites

Hello,

 

I took a look at the API and it's response to a query of /explore?near=houston&query=donuts&limit=10&[OATHKEY]

 

What I found is individual vendor information is stored at $json['response']['groups'][0]['items']

 

So my code is as follows (feel free to make it pretty)

$json_response = file_get_contents("https://api.foursquare.com/v2/venues/explore?near=houston&query=donuts&limit=10&oauth_token=TOKEN HERE"); 


$json =json_decode($json_response,1);

if(shuffle($json['response']['groups'][0]['items'])){
    foreach ($json['response']['groups'][0]['items'] as $item){
        print_r($item);
    }
}
Edited by valandor
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.