1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Array Help

Discussion in 'HTML, Graphics & Programming' started by AndyCr15, Apr 23, 2018.

  1. AndyCr15

    Soldato

    Joined: Apr 28, 2011

    Posts: 7,026

    Location: London, UK

    Hi, I've been dabbling in PHP for months now so I know I should know this, but can anyone confirm I would be doing this right if I made an array like this -

    Code:
    $item = array(
    "date" => $date,
    "name" => $name,
    "comment" => $comment
    );
    Then added it to an array like this -

    Code:
    array_push($items, $item);
    Would I then access the comments for example, like this -

    Code:
    $thisComment = $items[$x]['comment'];
    And how would I sort the items by 'date', given the date format goes yyyy-mm-dd, so should work as a straight forward sort?

    Thanks!
     
  2. Dup

    Sgarrista

    Joined: Mar 10, 2006

    Posts: 9,579

    Location: East Lancs

  3. moogle

    Wise Guy

    Joined: Nov 4, 2006

    Posts: 2,103

    Location: London

    Yep, a sort should do it. As the explanation in SO says, you need to convert the dates to numerical timestamps if you want to sort by actual date. Although having the date in yyyy-mm-dd should result in the same thing.
     
  4. AndyCr15

    Soldato

    Joined: Apr 28, 2011

    Posts: 7,026

    Location: London, UK

    Great, thanks.