I need to reorder an array at random but I am not sure what the best/cleanest/fastest way is to do this.
So what I am trying to achieve is the following. Let's say I have an array that looks like this:
$array = array(4, 4, 4, 4, 6, 6, 6, 6, 8, 8, 10, 10, 20, 40, 60);
My goal is to get something like this but at random:
$array = array(6, 4, 4, 10, 4, 6, 4, 6, 60, 6, 8, 6, 10, 40, 8, 20);
Here's what I've been trying but it doesn't seem to be working as intended:
$array = array(4, 4, 4, 4, 6, 6, 6, 6, 8, 8, 10, 10, 20, 40, 60);
$newArray = array();
$randomNumber = rand(0 , 14);
for ($x = 0; $x <= 15; $x++) {
$newArray[$x] = $array[$randomNumber];
}
Many thanks in advance to anyone who can help me out :)
Use shuffle() function.
shuffle($array);
http://php.net/manual/en/function.shuffle.php
$array = array(4, 4, 4, 4, 6, 6, 6, 6, 8, 8, 10, 10, 20, 40, 60);
Check the output of previous array
echo "<pre>";
print_r($array);
echo "</pre>";
Shuffling previous array & check output again.
shuffle($array);
print_r($array);
Now run a foreach loop like
foreach($array as $item){
echo $item;
}
Note: You don't need to store shuffle data to new array.
Related
I have custom page where there are 10 posts showing right now, what i need to show first 3 random then next 4-7 again random and 8-10 again random
Is their any way i can manage in the while loop
<?php
$count = 1;
while ( $loop->have_posts() ) : $loop->the_post();
echo the_title();
$count++;
endwhile;
?>
Thanks
If I understood you correctly, this should get you close to what you want.
Put your posts into an array first:
$posts = array();
while ( $loop->have_posts() ) {
$loop->the_post();
array_push($posts, $post);
}
Then sort that array. I'll demonstrate with 0-9:
$array = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$first = array_slice($array, 0, 3);
$second = array_slice($array, 3, 4);
$third = array_slice($array, 7, 3);
shuffle($first);
shuffle($second);
shuffle($third);
$newarray = array_merge($first, $second, $third);
print join(", ", $array) . "\n" . join(", ", $newarray) . "\n";
Which will lead to a random-ish sorting of the array while keeping "blocks" (top, middle, bottom) in the same order:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
1, 2, 0, 6, 5, 4, 3, 8, 7, 9
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
0, 2, 1, 3, 4, 5, 6, 9, 8, 7
Putting it all together:
$posts = array();
while ( $loop->have_posts() ) {
$loop->the_post();
array_push($posts, $post);
}
$first = array_slice($posts, 0, 3);
$second = array_slice($posts, 3, 4);
$third = array_slice($posts, 7, 3);
shuffle($first);
shuffle($second);
shuffle($third);
$newposts = array_merge($first, $second, $third);
foreach($newposts as $mypost) {
print $mypost->post_title . "<br />\n";
}
note that I erroneously had written push $posts, $post; instead of array_push($posts, $post);, I have written a lot of Perl lately, and it shows.
First time posting on stackoverflow.
After printing the main array, I have managed to highlight the values that are found in the second one, but I also want to print the number of times that the duplicate occurs in brackets at the same time. I have run out of the ideas on how to do that last part, I get stuck in multiple loops and other problems. I will paste here what's working for now.
The code:
$main = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20 );
$secondary = array( 1, 6, 10, 6, 17, 6, 17, 20 );
foreach ( $main as $number )
{
if ( in_array( $number, $secondary ) )
{
echo $item;
// this one is supposed to be highlighted, but the html code disappears on stackoverflow
/* this is where the number of duplicates should appear in bracket, example:
highlighted number( number of duplicates ) */
}
else
{
echo $item;
// this one is simple
}
}
EXPECTED RESULT:
1(1), 2, 3, 4, 5, 6(3), 7, 8, 9, 10(1), 11, 12, 13, 14, 15, 16, 17(2), 18, 19, 20(1)
Basically the brackets contain the number of times that the value is found in the second array, aside from being colored, but I can't paste the html code for some reason. Sorry for not making the expected result more clear !
PROBLEM SOLVED:
Thanks to everyone for your help, first time using this website, didn't expect such a quick response from you guys. Thank you very much !
You need to get the count values of your secondary array first using array_count_values. This is what you can acquire as
$main = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
$secondary = array(1, 6, 10, 6, 17, 6, 17, 20);
$count_values = array_count_values($secondary);
foreach ($main as $key => $value) {
if (in_array($value, $secondary)) {
echo $value . "<strong>(" . $count_values[$value] . ")</strong>";
echo ( ++$key == count($main)) ? '' : ',';
} else {
echo $value;
echo ( ++$key == count($main)) ? '' : ',';
}
}
Output:
1(1),2,3,4,5,6(3),7,8,9,10(1),11,12,13,14,15,16,17(2),18,19,20(1)
Assuming $secondary is the one with the dupes, you should go about it the other way:
$dupes = array();
foreach($secondary as $number) {
if (in_array($number, $main)) {
$dupes[$number]++;
}
}
var_dump($dupes);
<?php
$main = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12, 13, 14, 15, 16, 17,18,19,20);
$secondary = array( 1, 6, 10, 6, 17, 6, 17, 20 );
$result =array();
foreach($main as $key => $value){
$i=0;
foreach($secondary as $key1 => $value1){
if($value == $value1){
$i++;
}
$result[$value] = $i;
}
}
$resultString ='';
foreach($result as $key => $value){
$resultString .=$key.'('.$value.'),' ;
}
echo trim($resultString,',');
?>
Result:
1(1),2(0),3(0),4(0),5(0),6(3),7(0),8(0),9(0),10(1),11(0),12(0),13(0),14(0),15(0),16(0),17(2),18(0),19(0),20(1)
I have 52 weeks array's and each week array has a sub array with 9 values.
now I need to add a value 0 at the begin of each array and every next week I need 1 value more.
For example (notice that the 0-8 will be in a for loop)
$vruchtzettings_week["week1"][0-8] = 1, 2, 3, 4, 5, 6, 7, 8, 9
$vruchtzettings_week["week2"][0-8] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
$vruchtzettings_week["week3"][0-8] = 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
$vruchtzettings_week["week4"][0-8] = 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Though I can't really test it, I believe this would do it for you. What you're doing is really convoluted.
$week = 1;
while ($week <= 52) {
$sum = 0;
for ($sub = 0; $sub < 9; $sub++, $week++;) {
$totaal_vruchtzetting_week[$week] = $totaal["week$week"][$sub] + $sum;
$sum += $totaal["week$week"][$sub];
}
}
Like I said, you will probably have to tweek this a little. But it will get you started.
Is there a neat way to split an array into chunks based on an array of lengths?
Input:
$start = range(0, 30);
$length = [3, 7, 2, 12, 6];
Desired Output:
[
[0, 1, 2], // 3
[4, 5, 6, 7, 8, 9, 10], // 7
[11, 12], // 2
[13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], // 12
[25, 26, 27, 28, 29, 30], // 6
];
Using array_splice:
$target = array(); // or use [] in PHP 5.4
foreach($length as $i) {
$target[] = array_splice($start, 0, $i);
}
Try it.
Be advised, this changes $start!
This is very easily accomplished with the following:
Code:
$target = array();
$offset = 0;
foreach ($length as $lengthValue) {
$target[] = array_slice($start, $offset, $lengthValue);
$offset += $lengthValue;
}
var_dump($target);
Explanation:
What you are doing here is using the array_slice() method (very similar to the substr) to extract the portion of the array and then inserting it into the target array. Incrementing the offset each time allows the function to remember which one to use next time.
Hay i have an array consisting for 6 random numbers, here's an example
[4,8,12,22,23,43]
I also have 100 arrays containing 6 numbers, these are all random a few examples could be
[5,8,15,47,32,48]
[3,4,8,12,33,42]
[8,12,26,55,43,33]
[4,63,45,23,45,55] ...
I want to see how many times (out of the array of 100) these numbers match at least 3 from the top array. As you can guess this is a lottery experiment.
As you can see array number 3 matches 3 numbers from the top array.
Any ideas how do do this? With perhaps an option to see if 4 numbers matched.
$master_array = array(4, 8, 12, 22, 23, 43);
$arrays = array(array(5, 8, 15, 47, 32, 48),
array(3, 4, 8, 12, 33, 42),
array(8, 12, 26, 55, 43, 33),
array(4, 63, 45, 23, 45, 55));
foreach ($arrays as $arr)
{
$intersect = array_intersect($master_array, $arr);
if (count($intersect)==3) print 'Match: '.print_r($arr, true).PHP_EOL;
}
maybe smth like this:
$winner = [4,8,12,22,23,43];
$arrays = //all your 100 arrays
$i = 0; // number of matches
foreach ($arrays as $array)
{
$result = array_intersect($array, $winner);
if (count($result) >= 3) $i++;
}