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…
$first= range(1, 5);
$second = range(6, 11);
shuffle($first);
shuffle($second);
echo join(', ', $first)+" : "join(', ', $second);
Am I on the right track?
this works…
$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
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
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>';
?>
thanks for the hint, I used arr_merge()
$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?
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();
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 )
ok, heres the 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
N9ne
June 9, 2014, 2:33pm
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.
thats kind of where I get stuck, is this ok?
$third = array([$first][$second]);
shuffle($third);
join(', '$third);
$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
$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
think i got it,
$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>';
lukeurtnowski:
think i got it,
$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.