Jeisson Posted October 13, 2015 Share Posted October 13, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/ Share on other sites More sharing options...
benanamen Posted October 13, 2015 Share Posted October 13, 2015 Randomize $results Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523206 Share on other sites More sharing options...
Barand Posted October 13, 2015 Share Posted October 13, 2015 Post the json data (results) so we can see what you are working with. Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523207 Share on other sites More sharing options...
Jeisson Posted October 13, 2015 Author Share Posted October 13, 2015 Randomize $resultsHow do I do that? shuffle($results) gives error Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523209 Share on other sites More sharing options...
benanamen Posted October 13, 2015 Share Posted October 13, 2015 I assume $results is from an SQL query so you would do ORDER BY RAND() Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523211 Share on other sites More sharing options...
Jeisson Posted October 13, 2015 Author Share Posted October 13, 2015 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" ] } } ] } } Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523212 Share on other sites More sharing options...
Jeisson Posted October 13, 2015 Author Share Posted October 13, 2015 ok now I see. My mistake. it is from an api that gives json. So no sql, sorry. Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523213 Share on other sites More sharing options...
Barand Posted October 13, 2015 Share Posted October 13, 2015 Are you sure that json is correct - gives my a syntax error when attempting to decode? Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523216 Share on other sites More sharing options...
scootstah Posted October 13, 2015 Share Posted October 13, 2015 (edited) 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 October 13, 2015 by scootstah Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523217 Share on other sites More sharing options...
Barand Posted October 13, 2015 Share Posted October 13, 2015 These are the only groups I found $a = json_decode($j,1); $groups = []; foreach ($a['response']['venues'] as $v) { $groups[] = $v['hereNow']['groups']; } echo '<pre>',print_r($groups, true),'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523222 Share on other sites More sharing options...
benanamen Posted October 13, 2015 Share Posted October 13, 2015 Where are the API docs? That would be a good start as to what can easily be done. Post a link to the API. Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523223 Share on other sites More sharing options...
Jeisson Posted October 13, 2015 Author Share Posted October 13, 2015 (edited) 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 October 13, 2015 by Jeisson Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523225 Share on other sites More sharing options...
valandor Posted October 13, 2015 Share Posted October 13, 2015 (edited) 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 October 13, 2015 by valandor Quote Link to comment https://forums.phpfreaks.com/topic/298577-randomizing-shuffle/#findComment-1523234 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.