winder Posted July 30, 2008 Share Posted July 30, 2008 hello all! i'm trying to write a php function that shuffles array's columns. Let me give you an example to clear things out... if we have a table like this.... a b c 1 2 3 after the shuffle, a must be in same column as 1, b in same as 2 etc.... like this maybe... c a b 3 1 2 i think you get the point... I found out about built-in php function shuffle()... if i could somehow make this function to "remember" the exact same shuffle that did in one row...then it would be easy. I would apply the same shuffle in all the other rows...so columns are intact. But i'm afraid this isn't possible. So is there any built-in function that i can use? Or i have to write it all by myself? I would really be thankful for any advice, tip... thanks in advance! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 How is the array stored? Please show the output of print_r($yourarray); but substitute the correct array name obviously. Quote Link to comment Share on other sites More sharing options...
unkwntech Posted July 30, 2008 Share Posted July 30, 2008 You can probably just use the shuffle function. http://www.php.net/shuffle Quote Link to comment Share on other sites More sharing options...
winder Posted July 30, 2008 Author Share Posted July 30, 2008 How is the array stored? Please show the output of print_r($yourarray); but substitute the correct array name obviously. this is what i get from print_r... Array ( [0] => Array ( [0] => 14 [1] => 14 [2] => 14 [3] => 14 [4] => 14 [5] => 14 [6] => 14 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) [2] => Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 1 [4] => 1 [5] => 2 [6] => 3 ) [3] => Array ( [0] => -1 [1] => -1 [2] => -1 [3] => -1 [4] => -1 [5] => -1 [6] => -1 ) ) this is the same as the following right? 14141414141414 1234567 1211123 -1-1-1-1-1-1-1 You can probably just use the shuffle function. http://www.php.net/shuffle This is the first thing i looked at... but i can't see how this generic shuffle() function can help with this column restriction.... Quote Link to comment Share on other sites More sharing options...
Barand Posted July 30, 2008 Share Posted July 30, 2008 re-arrange your array $ar = array ( array (14, 1, 1, -1), array (14, 2, 2, -1), array (14, 3, 1, -1), array (14, 4, 1, -1), array (14, 5, 1, -1), array (14, 6, 2, -1), array (14, 7, 3, -1), ); Now you can shuffle and keep associated items together Quote Link to comment Share on other sites More sharing options...
winder Posted July 31, 2008 Author Share Posted July 31, 2008 OK. I sorted it out. I didn't have a clear view of how php manages arrays. I didn't have the correct orientation it seems shuffle() is more than enough Thanks guys for all you help! Quote Link to comment 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.