skip to Main Content

I am trying to split the arrays and name them or group them individually. When the form is submitted, all the inputs are in array. For example, <input type="text" name="fname[]" and so on. When I get the input in the controller they are group by fname 1 to 4. So, I wanted to split each of the first object in the array. Please find the code below of what I am getting now and my desire output I want:-

I have created a form where I am collecting the data and sending it to the controller.

// Family Members form
//this family form will add 3 more times as the total member in this fmaily are 4 who are still living
@for($i=1;$i<= $totalMembs;$i++) 
<input type="hidden" name="id[]" value="{{$i}}" >
<input type="text" name="fname[]" value="" >
<input type="text" name="lname[]" value="" >
<input type="date" name="dob[]" value="" >
<input type="radio" name="gender[{{$i}}]" id="genderM" value="male" >
<input type="radio" name="gender[{{$i}}]" id="genderF" value="female">
 <select name="nationality[]" id="nationality{{$i}}">
    <option value="">Please select</option>
    <option value="India">Indian</option>
    <option value="USA">USA</option>
    <option value="UK">UK</option>
    <option value="AUS">AUS</option>
 </select>
 <input type="text" name="birthPlace[]" value="">
 <select name="state[]" id="state{{$i}}">
    <option value="">Please select</option>
    <option value="MH">Maharashtra</option>
    <option value="GJ">Gujrat</option>
    <option value="DL">Delhi</option>
    <option value="MP">Madya Pradesh</option>
 </select>
 <select name="device[]" id="device{{$i}}">
    <option value="">Please select</option>
    <option value="Mobile">Mobile</option>
    <option value="Landline">Landline</option> 
 </select>
 <input type="text" name="countryCallingCode[]" value="+91">
 <input type="text" name="telnumber[]" value="">
 <input type="email" name="email[]" value="">
 @endfor

When I do dd($request->all();, I get everything in the group of firstname:

"fname" => array:4 [▶]
"sname" => array:4 [▶] 

The following is the output I am getting in JSON:

"_token": "8p8hu86Oi4FMZnak0si3wK629dDejyB07abxJIpJ",
"fname": [
"Kunal",
"Manisha",
"Vinay",
"Rishi"
],

"sname": [
"Parekh",
"Sevani",
"Parekh",
"Parekh"
],
"contactEmail": [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
],
"state": [
"MH",
"MH",
"MH",
"MH"
 ],
"contactTelCode": [
"91",
"91",
"91",
"91"
],
"phone": [
"0439378608",
"0778777767",
"0790123223",
"048293023"
],
"dateofBirth": [
"1981-10-18",
"1976-11-17",
"2009-04-12",
"182004-07-03"
],
"birthPlace": [
"Mumbai",
"Mumbai",
"Mumbai",
"Rajkot"
],
"genderType": {
"1": "male",
"2": "female",
"3": "male",
"4": "male"
},


"nationality": [
"Indian",
"Indian",
"Indian",
"Indian"
],

}

And the output I want is:

"family": [
    {
        "id": 1,
        "dateOfBirth":  "1981-10-18" ,
        "gender":   "male"  ,
        "name": {
            "firstName": [
                "Kunal" 
            ],
            "lastName": [
                "Parekh" 
            ]
        },
        "currentplace": {
            "nationality": "INDIAN" ,
            "birthPlace":  "Mumbai"  ,
        },
        "contact": {
            "state": "MH",
            "phones": {
                "deviceType": "MOBILE",
                "countryCallingCode": "91",
                "number": "980080076"
             }
                "emailAddress": "[email protected]"  
            }
            
        },
        
    },
     {
        "id": 2,
        "dateOfBirth":  "1976-11-17",
        "gender":   "female" , 
        "name": {
            "firstName": [
                "Manisha" 
            ],
            "lastName": [
                "Parekh" 
            ]
        },
        "currentplace": {
            "nationality": "INDIAN"  ,
            "birthPlace" : "Mumbai"  ,
        },
        "contact": {
            "state": "MH",
            "phones": {
                "deviceType": "MOBILE",
                "countryCallingCode": "91",
                "number": "980080075"
                "emailAddress": "[email protected]"  
            }
            
        },
        
    },
    {
        "id": 3,
        "dateOfBirth": "2009-04-12" , 
         
        "gender":  "male"  ,
        "name": {
            "firstName": [
                "Vinay" 
            ],
            "lastName": [
                "Parekh" 
            ]
        },
        "currentplace": {
            "nationality": "INDIAN"  ,
            "birthPlace":  "Mumbai" , 
        },
        "contact": {
            "state": "MH",
            "phones": {
                "deviceType": "MOBILE",
                "countryCallingCode": "91",
                "number": "980080074"
             }
                "emailAddress":   "[email protected]"  
            }
            
        },
        {
        "id": 4,
        "dateOfBirth": "2004-07-03" ,  
        "gender":  "1": "male" ,
        "name": {
            "firstName": [
                "Rishi" 
            ],
            "lastName": [
                "Parekh" 
            ]
        },
        "currentplace": {
            "nationality":  "INDIAN"  ,
            "birthPlace":    "Mumbai" ,
        },
        "contact": {
            "state": "MH",
            "phones": {
                "deviceType": "MOBILE",
                "countryCallingCode": "91",
                "number": "980080073"
             }
                "emailAddress": "[email protected]"  
            }
            
        },
 
    ]

Please let me know how to group the array as shown in the output above.

2

Answers


  1. Something like this, each object should have the same key in the fname array

    @foreach(range(1, $totalMembs) as $member)
      <input type="text" name="fname[{{ $member }}][id]">
      <input type="date" name="fname[{{ $member }}][dateOfBirth]">
      <input type="text" name="fname[{{ $member }}][gender]">
      <input type="text" name="fname[{{ $member }}][name][firstName]">
      <input type="text" name="fname[{{ $member }}][name][lastName]">
      <input type="text" name="fname[{{ $member }}][currentplace][nationality]">
      <input type="text" name="fname[{{ $member }}][currentplace][birthPlace]">
      <input type="text" name="fname[{{ $member }}][contact][state]">
      <input type="text" name="fname[{{ $member }}][contact][phones][deviceType]">
      <input type="text" name="fname[{{ $member }}][contact][phones][countryCallingCode]">
      <input type="text" name="fname[{{ $member }}][contact][phones][number]">
      <input type="text" name="fname[{{ $member }}][contact][emailAddress]">
    @endforeach
    
    Login or Signup to reply.
  2. I think you’re looking for something that matches your expected output:

    $arr = $request->all();
    
    $results = [];
    
    for ($i=0; $i < count($arr["fname"]); $i++) {
    
        $results["family"][] = [
            "id" => $i + 1,
            "dateOfBirth" => $arr["dateofBirth"][$i],
            "gender" => $arr["genderType"][$i + 1],
            "name" => [
                "firstName" => $arr["fname"][$i],
                "lastName" => $arr["sname"][$i]
            ],
            "currentplace" => [
                "nationality" => $arr["nationality"][$i],
                "birthPlace" => $arr["birthPlace"][$i]
            ],
            "contact" => [
                "state" => "MH",
                "phones" => [
                    "deviceType" => "MOBILE",
                    "countryCallingCode" => $arr["contactTelCode"][$i],
                    "number" => $arr["phone"][$i],
                ],
                "emailAddress" => $arr["contactEmail"][$i]
            ]
        ];
    }
     
    return $results;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search