Archive

Posts Tagged ‘howto’

How to: Insert a new field into a dynamic array

September 7th, 2009 Chuck No comments

I’ve been busy with work – deadlines and all, so it’s been a while since my last entry. For now I’ll just share a quick tip. How to Insert a new field into a dynamic array.

If you’ve been reading the code snippets I’ve been posting thus far, you’ll notice that I am doing this thing a lot. Anyway, here’s a little background on this situation.

Let’s say you just retrieved records from your database table and stored them into an associative array named $results, now you want to insert an extra field for each row but you don’t know how to to assign them since you don’t know how to walk through the array.

Let’s say you have this array:

Array
(
    [0] => Array
        (
            [user_id] => 1
            [user_name] => Chuck
            [user_level] => 3
            [birthdate] => 1984-09-15
        )

    [1] => Array
        (
            [user_id] => 2
            [user_name] => Blah
            [user_level] => 1
            [birthdate] => 2001-01-01
        )

    [2] => Array
        (
            [user_id] => 3
            [user_name] => New user
            [user_level] => 1
            [birthdate] => 2002-10-30
        )

    [3] => Array
        (
            [user_id] => 4
            [user_name] => Random guy
            [user_level] => 1
            [birthdate] => 1992-07-21
        )

)

This means, each row has the following fields – user_id, user_name, user_level, and birthdate.
Now, let’s say we wanted to create a new field named “age” and store it in the same array. Here’s my preferred method of doing this:

foreach($results as $index=>$row){
     $results[$index]['age'] = get_age($row['birthdate']);
}

Take note that to maintain the abstraction of the code, I just called a function named get_age();. Remember that there is no such function so you’ll have to code that in to make it work. For completeness sake, here’s how I’d probably do it:

// Accepts 2 parameters, $bday and $now. $bday is of
// the 'YYYY-MM-DD' format. The result is based on the
// difference between $now and $bday converted to
// years. $now is optional. If it is not supplied, it uses
// the current time. This is not exactly accurate since I
// just indiscriminately used 365.25 days in my calculation.
// To improve, I'll have to detect leap years and just
// add 1 day to that, instead of 1/4 days to every year.
function get_age($bday, $now = NULL){
     $bday = strtotime($bday);
     $now = ($now)?strtotime($now):mktime();
     return floor(($now - $bday)/(60*60*24*365.25));
}

Anyway, after doing all that, you will get this result:

Array
(
    [0] => Array
        (
            [user_id] => 1
            [user_name] => Chuck
            [user_level] => 3
            [birthdate] => 1984-09-15
            [age] => 24
        )

    [1] => Array
        (
            [user_id] => 2
            [user_name] => Blah
            [user_level] => 1
            [birthdate] => 2001-01-01
            [age] => 8
        )

    [2] => Array
        (
            [user_id] => 3
            [user_name] => New user
            [user_level] => 1
            [birthdate] => 2002-10-30
            [age] => 6
        )

    [3] => Array
        (
            [user_id] => 4
            [user_name] => Random guy
            [user_level] => 1
            [birthdate] => 1992-07-21
            [age] => 17
        )

)

As an aside, you could probably use array_walk() to achieve something similar, but this method is way simpler. I hope this has been helpful to you!

Categories: PHP and MySQL Tags: , , , ,