Jump to content

[SOLVED] array column shuffle


winder

Recommended Posts

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!

 

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

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

 

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.