President Obama Posted January 23, 2011 Share Posted January 23, 2011 Ok I'm really lost here, what I need to do is shuffle a 2D array. Now when I shuffle them I want to keep both columns aligned to say. Like I only want the index to change. Kinda like this: Index Id URL 0 [2 www.e] I want the bracketed stuff to stay aligned. 1 [6 www.b] 2 [12 www.c] 3 [60 www.h] But I want them in different positions on every page load. So when I echo array[1][0] it will always be different. Is this even possible? Quote Link to comment https://forums.phpfreaks.com/topic/225388-shuffle-multidimensional-array/ Share on other sites More sharing options...
trq Posted January 23, 2011 Share Posted January 23, 2011 Just use rand to choose your index. Quote Link to comment https://forums.phpfreaks.com/topic/225388-shuffle-multidimensional-array/#findComment-1163917 Share on other sites More sharing options...
President Obama Posted January 23, 2011 Author Share Posted January 23, 2011 eh should of explained my self better. What I'm doing is pulling 4 ads at random out of the db, but with each pull I want to add 1 to the views for that specific ad. As in the links I only want the URL but with the views I want the Id to compare to the database and find out which ad to add a view to. Also if I use rand 4 times it will pull duplicates which I also don't want. Hard to explain. Quote Link to comment https://forums.phpfreaks.com/topic/225388-shuffle-multidimensional-array/#findComment-1163920 Share on other sites More sharing options...
trq Posted January 23, 2011 Share Posted January 23, 2011 Sounds like you would be better of actually querying your database for 1 random record. Quote Link to comment https://forums.phpfreaks.com/topic/225388-shuffle-multidimensional-array/#findComment-1163922 Share on other sites More sharing options...
President Obama Posted January 23, 2011 Author Share Posted January 23, 2011 Explain please. Quote Link to comment https://forums.phpfreaks.com/topic/225388-shuffle-multidimensional-array/#findComment-1163923 Share on other sites More sharing options...
codefossa Posted January 23, 2011 Share Posted January 23, 2011 Sounds like you would be better of actually querying your database for 1 random record. Sounds like a rotator to me, which would prevent calling a duplicate before the loop finished. Quote Link to comment https://forums.phpfreaks.com/topic/225388-shuffle-multidimensional-array/#findComment-1163925 Share on other sites More sharing options...
President Obama Posted January 23, 2011 Author Share Posted January 23, 2011 Ok I found a way to totally ditch my multidimensional array. What I did is make 2 arrays one with the id's and one with the links then I used array_combine to combine them to a new array which has the indexes as the id's, now I can use shuffle on it and add views. Quote Link to comment https://forums.phpfreaks.com/topic/225388-shuffle-multidimensional-array/#findComment-1163971 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.