Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9032489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:59:36+00:00 2026-06-16T07:59:36+00:00

I have been trying to select random items from an array without repeating the

  • 0

I have been trying to select random items from an array without repeating the same item.

Sample Array

$images=array();
$images[]=array('img'=>'bands.jpg','cat'=>'bands.php');
$images[]=array('img'=>'cake.jpg','cat'=>'cakes.php');
$images[]=array('img'=>'catering.jpg','cat'=>'catering.php');
$images[]=array('img'=>'dj.jpg','cat'=>'djs.php');
$images[]=array('img'=>'dress.jpg','cat'=>'dress_attire.php');
$images[]=array('img'=>'limos.jpg','cat'=>'limos_transportaion.php');
$images[]=array('img'=>'photography.jpg','cat'=>'photography.php');
$images[]=array('img'=>'venues.jpg','cat'=>'venues.php');
$images[]=array('img'=>'wedding_planer.jpg','cat'=>'planning.php');

I have tried the following but for some reason it will not work correctly. It is only collecting the first items in the array to the count rendered.
// $adDisplay is a number between 1-9

$rand = array_rand($images, $adDisplay);
foreach($rand as $key => $value){
    echo'<a href="'.$images[$key]['cat'].'"><img src="img/banners/'.$images[$key]['img'].'" border="0" alt="" /></a>';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:59:37+00:00Added an answer on June 16, 2026 at 7:59 am

    Many ways of doing this, I’d probably shuffle and then slice the array:

    $images = array();
    $images[]=array('img'=>'bands.jpg','cat'=>'bands.php');
    $images[]=array('img'=>'cake.jpg','cat'=>'cakes.php');
    $images[]=array('img'=>'catering.jpg','cat'=>'catering.php');
    $images[]=array('img'=>'dj.jpg','cat'=>'djs.php');
    $images[]=array('img'=>'dress.jpg','cat'=>'dress_attire.php');
    $images[]=array('img'=>'limos.jpg','cat'=>'limos_transportaion.php');
    $images[]=array('img'=>'photography.jpg','cat'=>'photography.php');
    $images[]=array('img'=>'venues.jpg','cat'=>'venues.php');
    $images[]=array('img'=>'wedding_planer.jpg','cat'=>'planning.php');
    
    shuffle($images);
    
    $adDisplay = 5;
    
    foreach( array_slice($images,0,$adDisplay) as $image){
        echo '<a href="' . htmlspecialchars($image['cat']) . '">'
             . '<img src="img/banners/'
             . htmlspecialchars($image['img']) . ' border="0" alt="" />'
             . '</a>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying the following but always fails, roomTypeSQL = SELECT spftype FROM
I have been trying to apply user-select for both Opera 10.62 and IE9 without
I have been trying to run this query: my_post = db.GqlQuery(SELECT * FROM Post
I have been trying to retrieve data from Cassandra database using this command: select
I have the following mysql table. I have been trying to select all the
I have been trying to run this query to select a row by its
I have been trying to select text in a textarea programatically based on start
I have been trying off and on to populate an object from an XML
Salutations I have been trying to find a way to edit an Ember.Select box
I have been trying to move away from using DECODE to pivot rows in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.