CodingHero
CodingHero

Reputation: 683

Modify values of array / multidimensional array

Currently I want transform dates stored in mysql (dateime) to FR format, here is my current function :

public static function convert_dates($result){
    foreach ($result as $key => $value){
            if (count($result) != count($result, COUNT_RECURSIVE)){
                 foreach ($result[$key] as $key2 => $value2){
                    if(preg_match('#date#', $key2) && !empty($value2) && $value2!='0000-00-00'){
                        if (preg_match("#^\d{4}\-\d{2}-\d{2}$#", $value2)) {
                                $result[$key][$key2] = date("d/m/Y", strtotime($value2));
                        }elseif (preg_match("#^\d{4}\-\d{2}-\d{2} \d{2}\:\d{2}:\d{2}$#", $value2)) {
                                $result[$key][$key2] = date("d/m/Y H:i:s", strtotime($value2));
                        }
                    }
                 }
            }else{
                if(preg_match('#date#', $key) && !empty($value) && $value!='0000-00-00'){
                    if (preg_match("#^\d{4}\-\d{2}-\d{2}$#", $value)) {
                            $result[$key] = date("d/m/Y", strtotime($value));
                    }elseif (preg_match("#^\d{4}\-\d{2}-\d{2} \d{2}\:\d{2}:\d{2}$#", $value)) {
                            $result[$key] = date("d/m/Y H:i:s", strtotime($value));
                    }
                }
            }

    }
    return $result;
}

I call this function when I fetch array results from query, it can be single item or many items.

Actually it works, but there is any way to improve the code?

Thanks for answers.

Upvotes: 1

Views: 63

Answers (1)

Gareth Parker
Gareth Parker

Reputation: 5062

Yes, it can most certainly be improved. In a couple of ways. The fist is that you've duplicated the code for actually transforming the dates. The logic is the same, you're just trying to map recursively. The second is that you shouldn't use if (count($result) != count($result, COUNT_RECURSIVE)) to check if it's an array, you should use if (is_array($result)). Thirdly, though this seems less important, you should probably store your dates as DateTime objects, not as strings. But we'll ignore this for now, you can improve this later. So an improved version of the code would be

function transformDate($value) {
    if (preg_match("#^\d{4}\-\d{2}-\d{2}$#", $value)) {
        return date("d/m/Y", strtotime($value));
    } elseif (preg_match("#^\d{4}\-\d{2}-\d{2} \d{2}\:\d{2}:\d{2}$#", $value)) {
        return date("d/m/Y H:i:s", strtotime($value));
    }

    return $value;
}

public static function convert_dates($result) {
    foreach ($result as $key => $value) {
        if (is_array($value)) {
            //Have this function call itself reccursively
            $result[$key] = self::convert_dates($value);
        } else if (preg_match('#date#', $key) && !empty($value) && $value!='0000-00-00') {
            $result[$key] = transformDate($value);
        }
    }

    return $result;
}

I would suggest using the array_map function instead of actually modifying the array directly, but it's support for keys doesn't looks like its well documented. This should handle more than two dimensional arrays, as it should traverse down n dimensions

Upvotes: 1

Related Questions