Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

navneet's avatar

How do I join array childrens key to its parent in a multidimension array?

I am building a form that takes input with names as an array. It can be nested. The form is auto-generated by users so I don't control how many levels it can go.

<input name="question[city]" type="text" />
<input name="tester[profile][email]" type="email" />

Below is what I get in the controller on the form submit from the request->all().

^ array:15 [▼
  "name" => "Alexi"
  "question" => array:5 [▼
    city => "Mumbai"
    zip => "423423"
    region => "Bandra"
  ]
  "tester" => array:1 [▼
    "profile" => array:1 [▼
      "email" => "[email protected]"
    ]
  ]
]

I want to insert this into my JSON column in the database. How do I join children's to its parent key? I want to convert to like below JSON.

{
  "name": "Alexi",
  "question[city]": "Mumbai",
  "question[zip]": "423423",
  "question[region]": "Bandra",
  "tester[profile][email]": "[email protected]"
}

If it is not possible to store as array how can I display it in the blade like above ?

I have tried array flatten and array mapping but it is not going to all its depth.

0 likes
1 reply
vincent15000's avatar
{
  "name": "Alexi",
  "question": {
	"city": "Mumbai",
	"zip": "423423",
	"region": "Bandra"
  },
  "tester": {
	"profile": {
	  "email": "[email protected]"
	}
  }
}

Please or to participate in this conversation.