Modify data after query before sending to front-end

Posted 3 months ago by movepixels

I have an array of results that looks / similar like this:

$users = Array
(
    (0) => Array
        (
            (dob) => '1977-04-25',
            ......
        ),

    (1) => Array
        (
            (dob) => '1972-09-06',
            ......
        ),

    (2) => Array
        (
           (dob) => '1985-11-20',
            ......
        )
);

And what I need to do is find / replace the dob values with a Carbon function to return age. I do not want the actual data / dob to reach the front end so I need to alter it after the results but before the front end receives it. I know the key will be "dob" but can't figure out the correct way to loop thru the array properly and use a function to replace dob value with something like Carbon::createFromFormat('Y-m-d', $users[$k]['dob'])->year For the site privacy policy DOB can not be accessable so I need to restrict the value to an age only value (25 years old) So array_walk function with array_search but not having any luck.

Any ideas / insight would be appreciated.

Thanks,

Dave

Please sign in or create an account to participate in this conversation.