www.webdeveloper.com
Results 1 to 3 of 3

Thread: get 1 element from array at a time but no duplicate till reach array's end

  1. #1
    Join Date
    Dec 2006
    Posts
    145

    get 1 element from array at a time but no duplicate till reach array's end

    Hello,

    I'm working on some script and I stuck in one place.

    I have an array like this:

    PHP Code:
    $test_array = array('key 1' => 'value 1''key 2' => 'value 2''key 3' => 'value 3'); 
    I want to print one key & value at a time (from first to last). After passing random amount of seconds (let's say from range 1 to 10) print another key & value than another delay and print, delay and print (till the end of array). I don't want dupliacate print of the same records.

    I mean:

    Code:
    script prints: key1, value 1
    make random delay: (for ex.) 5 seconds
    than prints: key 2, value 2
    make another random delay: (for ex.) 3 seconds
    than prints: key 3, value 3
    Any suggestions will be appreciated.

  2. #2
    Join Date
    Nov 2012
    Location
    Jakarta
    Posts
    42

    Lightbulb

    Code:
    foreach ($test_array as $key => $value)
    {
         echo $key . "=>" . $value; //print out the values
         $iWait = rand(1,10); //randomize the seconds to wait, between 1 to 10
         sleep($iWait); //execute the waiting part.
    }
    Do check and read the PHP Manual. It can be downloaded from www.php.net

    High level languages do have basic functions that are quite easy to guess, just look up the functions from common sense.

    Good luck!

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,337
    If you want random elements from the array, just use shuffle() first.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles