1. ## combining two arrays?

Heres a pre-test question im trying to wrap my head around,

create a randomized array with values 1-11, where the first five elements and the last six elements are grouped together. Note: The order of the groups should also be randomized.

But im lost on, heres what im thinking..
PHP Code:
``` \$first= range(1, 5);\$second = range(6, 11);shuffle(\$first); shuffle(\$second);echo join(', ', \$first)+" : "join(', ', \$second);  ```
Am I on the right track?

2. this works..
PHP Code:
``` \$first= range(1, 5);\$second = range(6, 11);shuffle(\$first); shuffle(\$second);echo join(', ', \$first)." : ".join(', ', \$second);  ```
but how do I randomize the order of the 2
http://shores-rentals.com/luth/question7.php

3. Take a look at the PHP Manual on array functions:
http://www.php.net//manual/en/ref.array.php

After you have selected a suitable function, try this:
PHP Code:
``` <?php echo '<pre>';    \$first= range(1, 5);    \$second = range(6, 11);    shuffle(\$first);     shuffle(\$second);    \$third = array();    // join(', ', \$first)+" : "join(', ', \$second);          print_r(\$first);      echo '<br />';    print_r(\$second);       echo '<br />';    print_r(\$third);       echo '<br />';echo '</pre>';?> ```

4. thanks for the hint, I used arr_merge()
PHP Code:
```     \$first= range(1, 5);    \$second = range(6, 11);    shuffle(\$first);     shuffle(\$second);    \$third = array();    \$third = array_merge(\$first, \$second);    // join(', ', \$first)+" : "join(', ', \$second);          print_r(\$first);      echo '<br />';    print_r(\$second);       echo '<br />';    print_r(\$third);       echo '<br />';  ```
But now how do I shuffle the two arrays inside the merged one?

5. Looks as though you never tried the code that I supplied and only looked at the link so...

Try this and repeat for all arrays();
PHP Code:
``` echo '<pre>';    echo '<br />\$first = range(1, 5);<br />';         \$first= range(1, 5);        print_r(\$first);    echo '<br />shuffle(\$first); ';         shuffle(\$first);        print_r(\$first);echo '</pre>';  ```
Output:

\$first = range(1, 5);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)

shuffle(\$first); Array
(
[0] => 4
[1] => 1
[2] => 2
[3] => 5
[4] => 3 )

6. ok, heres the code
PHP Code:
``` echo '<pre>';    echo '<br />\$first = range(1, 5);<br />';         \$first= range(1, 5);        print_r(\$first);    echo '<br />shuffle(\$first); ';         shuffle(\$first);        print_r(\$first);echo '</pre>';  echo '<pre>';    echo '<br />\$second = range(6, 11);<br />';         \$second= range(6, 11);        print_r(\$second);    echo '<br />shuffle(\$second); ';         shuffle(\$second);        print_r(\$second);echo '</pre>';  echo '<pre>';        \$third= array_merge(\$first,\$second);        print_r(\$third);    echo '<br />shuffle(\$third); ';         shuffle(\$third);        print_r(\$third);echo '</pre>';  ```
but after I shuffle the third array, all the values get shuffled and not just the 2 groups.

Thanks for your help with this

7. When you merge two arrays, it just adds all the elements into one array. You need to create a third array which holds the original two arrays. Then shuffle that which will randomise the order of those arrays. Then merge the two arrays that are inside that array.

8. thats kind of where I get stuck, is this ok?
PHP Code:
```     \$third = array([\$first][\$second]);shuffle(\$third);join(', '\$third);  ```

9. PHP Code:
```     \$first = range(1, 5);    \$second = range(6, 11);    shuffle(\$first);     shuffle(\$second);\$third = array(\$first,\$second);shuffle(\$third);echo join(', ',\$first);echo '<br>';echo join(', ',\$second);echo '<br>';echo join(', ',\$third);  ```
produces
5, 4, 2, 1, 3
6, 11, 7, 8, 9, 10
Array, Array

10. PHP Code:
``` \$third = array(\$first,\$second);shuffle(\$third);array_merge(\$third);echo join(', ',\$first);echo '<br>';echo join(', ',\$second);echo '<pre>';print_r(\$third);echo '</pre>';  ```
produces
http://shores-rentals.com/luth/question7.php
bot im trying to put it into a line

11. think i got it,
PHP Code:
``` \$first = range(1, 5);\$second = range(6, 11);shuffle(\$first); shuffle(\$second);\$third = array(\$first,\$second);shuffle(\$third);array_merge(\$third);echo '<br><p>First Array:&nbsp;&nbsp;&nbsp;'.join(', ',\$first).'</p>';echo '<br><p>Second Array:&nbsp;&nbsp;&nbsp;'. join(', ',\$second) .'</p>';echo '<br><p>Combined Array:&nbsp;&nbsp;&nbsp;'. join(', ',\$third[0]) .'&nbsp;&nbsp;:&nbsp;&nbsp;'. join(', ',\$third[1]) .'</p>';  ```

12. Originally Posted by lukeurtnowski
think i got it,
PHP Code:
``` \$first = range(1, 5);\$second = range(6, 11);shuffle(\$first); shuffle(\$second);\$third = array(\$first,\$second);shuffle(\$third);array_merge(\$third);echo '<br><p>First Array:***'.join(', ',\$first).'</p>';echo '<br><p>Second Array:***'. join(', ',\$second) .'</p>';echo '<br><p>Combined Array:***'. join(', ',\$third[0]) .'**:**'. join(', ',\$third[1]) .'</p>';  ```
You can remove the call to array_merge, it isn't doing anything.

13. oh, thanks

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•