vendredi 19 février 2021

Insert array elements inside another array(main)

current response :

[
{
    "Games": [
        {
            "sub_service_name": "Pubg",
            "service_id": 567,
            
        },
        {
            "sub_service_name": "call of duty",
            "service_id": 568,
        },
        {
            "sub_service_name": "candy crush",
            "service_id": 569,
           
        },
        {
            "sub_service_name": "clash of clans",
            "service_id": 566,
            
        }
    ]

Expected response

{
    "Games": {
          Multiplayer:[
        {
            "sub_service_name": "Pubg",
            "service_id": 567,
            
        },
        {
            "sub_service_name": "call of duty",
            "service_id": 568,
        }
     ],
        Singleplayer:[
        {
            "sub_service_name": "candy crush",
            "service_id": 569,
            "service_amount": "500"
        },
        {
            "sub_service_name": "clash of clans",
            "service_id": 566,
            
        }
    ]

}

Anyone please tell me how can I include the mutiplayer and singleplayer array inside the main array Games. Please find the controller function below. There are two tables with main services where included Games and other main services and ID's. Sub service table contains sub services includes call of duty, pubg and so on.

   public function GamesList(){

    $res      = $mainservice->mainServiceData();   // id and main service name 
    $sub      = $subservice->subServiceDataOnly(); // sub service data

    foreach ($res as  $value) {

    $id   = $value->id;
    $name = $value->main_service_name;
    $result[$name] = $subservice->subServiceData($id);                           
    }

     $mainres[] = $result;
     $data['data']    = $mainres;
     return response()->json($data);

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire