How can I assign name to object in php?

I am integrating code below

        $data[] = array('id'=>$id);
        $data[] = (object) [  "first_name"=> "John", "last_name"=> "Doe"];
        $final_result = array('result'=> $data);
        print json_encode($final_result);

I am getting response like this in api

    "result": [
        {
          "id": "1",
         },
          {
                "first_name": "John",
                "last_name": "Doe",
             }
    ]

but I want it to look like below

       "result": [
        {
          "id": "1",
         },
         "emp_info":  {
                "first_name": "John",
                "last_name": "Doe",
             }
    ]

how can I do that ?

Won’t work, that’s invalid JSON. Arrays don’t have index keys. you have to use an object then. That involves that the first object also has a named index.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.