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 8840543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:29:16+00:00 2026-06-14T10:29:16+00:00

I currently have a piece of code that is selecting multiple values from an

  • 0

I currently have a piece of code that is selecting multiple values from an array and was just wondering how I can switch it over to only picking a single value from an array.

So far I’ve tried changing array_rand($status, rand(2,3)) to array_rand($status, rand(1,1)) but it gives me the error message ** Invalid argument supplied for foreach()**. How can I resolve it?

$dateStart = new DateTime();
$dateStart->setDate(2012, 10, 01);
$dateEnd = new DateTime();
$dateEnd->setDate(2012, 12, 01);

$dates = array();
while ( $dateStart < $dateEnd ) {
   $rand = array_rand($status, rand(2,3));
   $text = '';
   foreach ( $rand as $key ) $text .= $status[$key] . '<br />';

   printf("<li><div id='activity_date'>%s</div>
           <div id='activity_box'>
           <div id='activity_text'>" . $text . ' request</div></div></li>',

   $dateStart->format("d/m/Y")
   );

   $dateStart->modify(sprintf("+%d day",mt_rand(1, 10)));
}
  • 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-14T10:29:17+00:00Added an answer on June 14, 2026 at 10:29 am

    If you want to keep your foreach, you have to ensure that you get an array back from array_rand. One way would be like this:

    $rand[] = array_rand($status);
    foreach($rand as $key) { ... }
    

    If you’re always going to want only one random array member, then @Gianps has the better approach. Lose the foreach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a piece of code that is selecting random values from an
I have a piece of php code that reads values from a database resultset,
Currently I have a piece of code that works fine as long as there
I have a piece of code that is preprocessing submitted form. Currently it was
I currently have a piece of code that I am working on using function
To put the problem simply, I have a piece of code that is currently
I currently have a piece of code in my Android application that picks up
I currently have code that reads a recording in from the devices mic using
I have this piece of code that currently almost does what I need. It
Currently I have a small piece of PHP code that gets one random row

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.