Bits Please
Bits Please

Reputation: 897

Save data in json format using php

I wanted to save the data in this format into database option field:

{"type":"type_value"}

I am getting data in post varaibles like this

$type_value = $request->input('type_value');
$type = $request->input('type');

How Can I save this in database? I have tried this

$data['options'] = array($type,$type_value);

But by this it is saving in this format:

["Qualifiers","1"]

I even tried doing this:

$data['options'] = json_encode(array($type,$type_value));

Instead it is saving like this

"[\"Qualifiers\",\"1\"]"

how can I do this?

Upvotes: 0

Views: 650

Answers (1)

Yash Parekh
Yash Parekh

Reputation: 1529

You just have to change your array definition. Your array considers 2 different elements i.e type and type_value. So just make your array with key value pair and you are all set.

json_encode(array($type => $type_value))  

Check this :- Fiddle

Upvotes: 2

Related Questions