Grow your CSS skills. Land your dream job.

How do I get each value of an array and put it in separate variables.

  • # March 6, 2013 at 9:12 pm

    Hi guys, can anyone please help me out:
    How do I get each value of an array and put it in separate variables.

    here’s the array:

    Array ( [0] => 1946 [1] => 6 [2] => 10 )

    This is a birthday in an array, I’d like to get each value and put it in 3 variables which is the $Year, $Month, $Day. So I can output it as “Month Day, Year”.

    Please please help. Thanks.

    # March 6, 2013 at 10:05 pm

    Your array declaration is invalid. If you make it like:

    array(1946, 6, 10);

    or

    array(0 => 1946, 1 => 6, 2 => 10);

    if you must, you can then use

    $year = $whatever_array[0];

    # March 6, 2013 at 10:07 pm

    Actually got an answer for this problem. :D

    $selectbirthday = array ( [0] => 1946 [1] => 6 [2] => 10 );

    $resultyear = $selectbirthday[0];

    echo $resultyear;

    output is: 1946

    # March 7, 2013 at 6:06 am

    Really? What PHP version do you use because I definitely getting an error on that array syntax (PHP Version 5.3.13)

    # March 7, 2013 at 11:10 am

    @CrocoDillon, what you posted recently was correct. I actually forgot to mention that I fetched the values of the array from a database field and used “explode()” function to reference them individually. This is actually the output when I print_r “_array ( [0] => 1946 [1] => 6 [2] => 10 );_”

    But this is the correct way:

    $selectbirthday = array( 0 => 1946, 1 => 6, 2 => 10 );

    $resultyear = $selectbirthday[0];

    echo $resultyear;

    Thanks for the help! (thumbs up)

    # March 7, 2013 at 11:17 am

    You sure know how to puzzle my mind! I’m glad it’s solved :)

    __
    # March 7, 2013 at 11:37 pm

    you could also do like so:

    < ?php
    $ymd = array( 1946,6,10 );
    list( $year,$month,$day ) = $ymd;
    print “Year: $year”;

    [`list()`](http://php.net/list) is underappreciated… : )

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".