Json Respone include function in php : I have one PHP code which has JSON response.

I need to separate the **['name' => $message->getName(), 'mes' => $message->getMes(), 'update_time' => $message->getUpdateTime()->format('Y-m-d H:i:s')]** into a independent function.
When I need I can call it.

But, I don't know how to do.

this is my controller

       /**
         * @Route("/message/board/reset/{id}", name = "message_reset", requirements = {"id" = "\d+"})
         */
        public function editMeg($id)
        {
            $entityManager = $this->getDoctrine()->getEntityManager();
            $message = $entityManager->find('MegBundle:message', $id);
            $UserN = $message->getName();
            $UserM = $message->getMes();
            return new JsonResponse(['result' => 'ok', 'ret' => ['name' => $message->getName(), 'mes' => $message->getMes(), 'update_time' => $message->getUpdateTime()->format('Y-m-d H:i:s')]]);
        }

I hope this is reasonable and has ways to do it.

I appreciate any help.