Home > Software engineering >  Array PHP Concatenate in one Array
Array PHP Concatenate in one Array

Time:05-03

Hi guys I have this output

string(47) "[{"value": "["Mouse","Cable Lock","Headset"]"}]"

What I want is like this

"[
{
 "value" : "Mouse"
},
{
 "value": "Cable Lock"
},
{
 "value": "Headset"
}
]
"

this is my code

foreach($_POST['Acc'] as $accessories)
{
   $arrAccesories[] = $accessories;
}

var_dump('[{"value": "'.json_encode($arrAccesories).'"}]');

I need it to be encoded because the data I am going to supply must be a string. How to achieve this guys

CodePudding user response:

Add the value key in the array:

foreach($_POST['Acc'] as $accessories)
{
   $arrAccesories[]['value'] = $accessories;
}
var_dump(json_encode($arrAccesories));
  • Related