0

I have a multidimensional array like this one:

Array
(
    [site1] => Array
        (
            [0] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data
                )

            [1] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data

                )

            [2] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data

                )

    [site2] => Array
        (
            [0] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data
                )

            [1] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data
                )

            [2] => Array
                (
                    [0] => data
                    [1] => data
                    [2] => data
                    [3] => data
                )
         )
)

I am trying to randomize the data for each site ([site1], [site2]) but don't mix the data between sites. So it would be like a second tire randomization.

For example, after the randomization, the position [0] for [site1] would have different data, maybe the data from the earlier position [3].

Any ideas how to do it?

3 Answers 3

3

You map the shuffle function to the array:

$shuffle = function($array) {
    $r = shuffle($array);
    return $array;
};

$sites = array_map($shuffle, $sites);

Or:

foreach ($sites as &$site) shuffle($site);
unset($site);
Sign up to request clarification or add additional context in comments.

7 Comments

This looks classy but it does not work; maybe I can give you a link to my site and a print_r to take a look at the actual array? The site has light adult content so I hope you won't mind.
Well you could also just tell what does not work in your case ;)
After I apply the above function I get: Array ( [site1] => 1 [site2] => 1 [site3] => 1 ) All the data for each site has gone.
This is because shuffle returning either true or false and not the "randomized" array. So array_map will use the return value of shuffle as the value.
Ty clenfort, any ideas on what to do?
|
1
foreach($sites as $i => $site) {
   shuffle($sites[$i]);
}

Comments

0

Another, (not as good as shuffle) way ;-)

foreach ($site as $key => $data) {
  $values = array();
  $rand   = array_rand($data, count($data));
  for ($i = 0; $i < count($rand); $i++)) {
    $values[] = $site[$key][$rand[$i]];  
  }
  $site[$key] = $values;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.