phparrayscodeigniter

Push data into a specific subarray


I have a function to show a JSON array containing multiple registration id and data. I am using CodeIgniter. I tried to use notification using this library.

public function send_notification($id_user, $message) {
  if ($this->muser->cek_follower($id_user) == TRUE) {
    $query = $this->muser->get_list_follower($id_user);
    $feedback['registration_id'] = array();
    foreach ($query->result() as $row) {
      $query_list_user = $this->muser->get_all_name_user_from_id($row->id_follower);
      if ($query_list_user->num_rows() > 0) {
        $row = $query_list_user->row();
        $response= $row->regid;
      } else {
        $feedback = 0;
      }
      array_push($feedback, $response);
    }
    $feedback['data'] = $message;
    echo json_encode($feedback);
  } else {
    $feedback = 0;
    echo json_encode($feedback);
  }
}

I tried GET from this url, but the output is like this

{
    "0": "XX3",
    "1": "XX4",
    "2": "XX2",
    "registration_id": [],
    "data": "ada"
}

How to make an output like this?

{
    "registration_id": [
        "XX3",
        "XX4",
        "XX2"
    ],
    "data": "ada"
}

Solution

  • You're pushing $response data directly to the parent array $feedback, instead of actually pushing it to $feedback['registration_id].

    So; replace this:

    array_push($feedback, $response);  
    

    with this:

    array_push($feedback['registration_id'], $response);