L
L
lexstile2021-07-11 21:58:16
PHP
lexstile, 2021-07-11 21:58:16

What are the ways to add an optional field to an array?

Task: if field === null, don't send to front.
Perhaps there are some elegant solutions?

So far I've done this:

$user = (object)$this->model->getUser($pay_id)[0];
          
          $data = (object)[
            'id' => $user->id,
            'first_name' => $user->first_name,
            'last_name' => $user->last_name,
            'photo' => $user->photo,
          ];
          
          if ($user->activity) {
            $data->activity = $user->activity;
          }
          
          $this->helper->sendResponse(['user' => $data]);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question