I am trying to loop through an array for a max amount of times in a loop, but I'm trying to figure out how to have it not output the same set twice.
Below is the crux of the code, stripping out all the superfluous code.
Previously when I looked at this code it had an unset() which I have commented out. I don't think it was doing what it is supposed to, and I really can't think of a way to stop it without adding an id to the individual arrays.
I have built this dummy array:
$array = array(
array(
'title' => 'Bill',
'url' => 'example.com/?u=1',
'img' => 'image1.jpg'
),
array(
'title' => 'Frank',
'url' => 'example.com/?u=2',
'img' => 'image1.jpg'
),
array(
'title' => 'Jill',
'url' => 'example.com/?u=3',
'img' => 'image1.jpg'
)
...
);
and I have the output page:
$a = 0;
$m = 2; // output max two set via $max
foreach( $users as $user ) {
$a++;
// unset( $users[$random_out] );
if( $a < $m ) {
$random_out = array_rand( $users );
$user_title = $users[$random_out]['title'];
$user_url = $users[$random_out]['url'];
$user_img = $users[$random_outid]['img'];
echo '<a href="' . $user_url . '">';
echo '<img src="' . $user_img . '">';
echo $user_title;
echo '</a>';
}
}