Show top 3 random posts on Wordpress - php

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.

Related

PHP reorder an array at random

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.

How To Get The Original Key From Sliced Array?

I have this code :
<?php
$order_list = array ( array ("tangible", 1, 8, 33, 19000),
array ("tangible", 1, 9, 8, 19000),
array ("tangible", 1, 3, 24, 19000),
array ("tangible", 1, 2, 10, NULL),
array ("tangible", 1, 17, 11, 28000));
$num = 2;
foreach(array_slice($order_list, $num) as $key => $value) {
echo $key.'=>'.$value[2].'<br>';
}
?>
and the result is this :
0=>3
1=>2
2=>17
the problem is... $value = 3 has $key = 0, while in the $order_list this value has $key = 2.
so, I'm expecting $key from $order_list based on value sliced. how to do that?
thank you.
....and the manual says: http://us3.php.net/manual/en/function.array-slice.php
The fourth argument, preserve_keys.

Counting, highlighting and printing the duplicates between two arrays

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)

Multiply two array

I have an two array like this:
$wij = array(0.25, 0.30, 0.25. 0.15, 0.5);
$nij = array(
array(3, 3, 2, 1, 2),
array(2, 2, 3, 2, 1),
array(1, 3, 2, 2, 1));
$rij = array();
I want to multiplying a value from wij array-variable into each nij array and join the result into rij array-variable, because $nij array always have more than 3 array than from the example. I dont have any clue just using for-loops in 1 looping. Please give me an example
if you want just add the values to $rij array use the code below:
$wij = array(0.25, 0.30, 0.25, 0.15, 0.5);
$nij = array( array(3, 3, 2, 1, 2), array(2, 2, 3, 2, 1), array(1, 3, 2, 2, 1));
$rij = array();
foreach($nij as $arr) {
foreach($arr as $val) {
foreach($wij as $multiplier) {
$rij[] = $val * $multiplier;
}
}
}
print_r($rij);

MAX & MIN values on array using PHP

Does anybody knows how can I get the max and min value of the 2nd and 3rd columns in PHP?
$ar = array(array(1, 10, 9.0, 'HELLO'),
array(1, 11, 12.9, 'HELLO'),
array(3, 12, 10.9, 'HELLO'));
Output should be like:
max(12.9)
min(10)
Another option
<?php
function array_rotate( $array )
{
$rotated = array();
foreach ( $array as $rowIndex => $col )
{
foreach ( $col as $colIndex => $value )
{
$rotated[$colIndex][$rowIndex] = $value;
}
}
return $rotated;
}
$ar = array(array(1, 10, 9.0, 'HELLO'),
array(1, 11, 12.9, 'HELLO'),
array(3, 12, 10.9, 'HELLO'));
$ar = array_rotate( $ar );
echo max( $ar[2] ), "\n", min( $ar[1] );
<?php
$ar = array(array(1, 10, 9.0, 'HELLO'),
array(1, 11, 12.9, 'HELLO'),
array(3, 12, 10.9, 'HELLO'));
function col($tbl,$col){
$ret = array();
foreach ($tbl as $row){
$ret[count($ret)+1] = $row[$col];
}
return $ret;
}
print (max(col($ar,2))."\n");
print (min(col($ar,1))."\n");
?>
is this what you look for? I guess its not the most efficient way.

Categories