Results 1 to 6 of 6
  1. #1
    Full Member gcarson's Avatar
    Join Date
    November 13th, 2009
    Posts
    383
    PHP & Arrays
    Alright, I'm stuck. I've tried hundreds of combinations but can't seem to figure this out. I'm not even sure if this is possible. I tried searching for an answer but given the common terms involved, it made it difficult at best.

    Basically, I have an array that I'm using on a script. I want to add to the array an element that has words as well as today's date. Here's what I have:

    PHP Code:
    $today=date("d/m/Y",time());
    $temp_array = array(
        
    'It was added on '.$today,
        
    'I went on '.$today
    ); 
    That's the gist of it. I want to add today's date (or any date for that matter), to an array. Is it possible to add a variable in an array element like this?

  2. #2
    Comfortably Numb John Powell's Avatar
    Join Date
    October 17th, 2005
    Location
    Bayou Country, LA
    Posts
    3,432
    PHP Code:
    $today=date("d/m/Y",time());
    $temp_array = array(
        
    'a'=>"It was added on $today",
        
    'b'=>"I went on $today"
    ); 
    echo 
    $temp_array['a'].'<br />';
    echo 
    $temp_array['b']; 
    This works.


  3. #3
    Full Member gcarson's Avatar
    Join Date
    November 13th, 2009
    Posts
    383
    John. Once again. Thank you. As someone who has looked at your past posts, you have definitely contributed some great stuff over the years.

    Hmm. I see that works in that situation. Now, how can I get it to do this? Basically, I'm setting up an array of around 100 different little phrases for when a user enters some info on my site. I'm then picking a random element from the array using this function:

    PHP Code:
    $reply $temp_array[array_rand($temp_array)];
    echo 
    $reply
    When I do this, the date portion is blank.
    I tried adding [] after $temp_array but that didn't work. More plugging away I guess!

  4. #4
    Full Member gcarson's Avatar
    Join Date
    November 13th, 2009
    Posts
    383
    Ok. I decided to choose the phrase this way. I just randomly pick a number. So if I have 10 comments, I have it randomly pick a number between 1 and 10. Instead of using 'a' 'b', I'm using numbers. Seems to work fine in the example above. I add the code to my script and it works fine. However, I when I put a little function for this, for whatever reason, it doesn't seem to work? Even if I just try to echo the results. I'll keep plugging away and report back!
    Last edited by gcarson; November 8th, 2010 at 06:05 PM.

  5. #5
    Full Member gcarson's Avatar
    Join Date
    November 13th, 2009
    Posts
    383
    Edit.. nevermind. I'm pretty much an idiot. The $today variable is defined outside of the function. So when I tried adding it in the function, it came up blank. I couldn't for the life of me figure out why it worked outside the function but not inside. So it worked the first way I had but since I never defined the date inside the function, it was coming up blank.

    Thanks again John for your help!
    Last edited by gcarson; November 8th, 2010 at 06:17 PM.

  6. #6
    Comfortably Numb John Powell's Avatar
    Join Date
    October 17th, 2005
    Location
    Bayou Country, LA
    Posts
    3,432
    Glad you got it.


  7. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. HTML & SEO vs. PHP.....
    By Steve Williams in forum PopShops
    Replies: 52
    Last Post: March 21st, 2008, 10:31 AM
  2. Differences between JScript & PHP
    By ELO in forum GoldenCAN
    Replies: 6
    Last Post: September 7th, 2006, 08:24 PM
  3. Random arrays of images
    By Rhia7 in forum Programming / Datafeeds / Tools
    Replies: 10
    Last Post: April 15th, 2006, 03:25 AM
  4. cgi & php best uses
    By SpaceBandit in forum Programming / Datafeeds / Tools
    Replies: 11
    Last Post: January 7th, 2002, 05:52 PM

Posting Permissions

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