본문 바로가기

PHP

배열에 재귀로 함수 실행하기

 

    public static function recursionFuntion($data, $arrDepth = 1)
    {
        return array_map(function ($item) use ($arrDepth) {
            if (is_array($item) === true) { 
                //하위배열 유무 체크
                $arrDepth++;
                $item = self::recursionFuntion($item, $arrDepth);
            } elseif (gettype($item) === 'string' || gettype($item) === 'integer') { 
                 //문자나 숫자면 실행
                 $item = htmlspecialchars_decode($item);
            }
            return $item;
        }, $data);
    }