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?
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
Related
I'm saving an array to the db but when I do the array is getting quotes around it from the start to end
e.g
"[{"id":"1","country":"New Zealand","shipping_rate":"1"},{"id":"2","country":"Australia","shipping_rate":"2"}]"
the array shouldn't be quoted at the start and the end when saving but this is happening. When I check my model on the shipping field that is an array I tried to trim it but it's says it can't because it says it's an array, so the problem is when it's being saved to the db for that field
The array should just look like this when being saved unquoted
[{"id":"1","country":"New Zealand","shipping_rate":"1"},{"id":"2","country":"Australia","shipping_rate":"2"}]
mysql doesn't save arrays, it saves strings. When saving an array it is by design that it serializes the array into a string.
If you are using MySql 5.7.8 or later you can use the JSON data type.
https://dev.mysql.com/doc/refman/5.7/en/json.html
You can't save an array in the mysql directly it will throws exception, you should convert it in a string(use json_encode()) and save it in mysql in either varachar/text type field.
Again when you get the data from db, convert it in an array(use json_decode()) and then use it.
u must convert json string to array :
<?php
$json_string= '[{"id":"1","country":"New Zealand","shipping_rate":"1"},{"id":"2","country":"Australia","shipping_rate":"2"}]';
$array = json_decode($json_string,true);
?>
then make a loop with foreach and enter a query in foreach :
<?php
foreach($array as $data){
$id = $data['id'];
$country = $data['country'];
$save= mysql_query("insert into your_database values("$id", "$country")";
}
?>
i home this help
I am trying to unset a value from test_bots.json and save it back, but somehow the data format is being changed in the process.
test_bots.json contains this JSON array:
["John","Vladimir","Toni","Joshua","Jessica"]
My code looks like this:
$good = 'Toni';
$good_arr = file_get_contents('test_bots.json');
$good_arr = json_decode($good_arr);
if(in_array($good, $good_arr)){
$key = array_search($good, $good_arr);
unset($good_arr[$key]);
$good_arr2 = json_encode($good_arr);
file_put_contents('test_bots.json',$good_arr2);
}
The output that's saved is:
{"0":"John","1":"Vladimir","3":"Joshua","4":"Jessica"}
but I want the output to look like:
["John","Vladimir","Joshua","Jessica"]
I tried to unserialize the array before saving it, but it's not working.
Why is this happening?
In order for json_encode to convert a PHP array with numeric keys to a JSON array rather than a JSON object, the keys must be sequential. (See example #4 in the PHP manual for json_encode.)
You can accomplish this in your code by using array_values, which will reindex the array after you have removed one of the items.
$good_arr2 = json_encode(array_values($good_arr));
Confusing title, the basics are that I'm saving a fully sorted and ordered multidimensional array from a script and into MySQL. I then, on another page, pull it from the database and unserialize it, and then proceed to print it out with this,
$s = "SELECT * FROM gator_historical_data WHERE channelid = '{$chanid}'";
$r = $link->query($s);
$comboarray = array();
while ($row = mysqli_fetch_assoc($r)) {
$comboarray[] = unserialize($row['dataarray']);
}
foreach ($comboarray as $item) {
$desc = $item['content']['description'];
$title = $item['content']['title'];
$datetime = $item['datetime'];
// ... ^^^ problems getting array data
}
The problem is that it doesn't take the full array from MySQL, only the first entry and thus only prints the first 'array'. So where the returned value from dataarray looks like this (var_dump): http://pastebin.com/raw.php?i=Z0jy55sM the data stored into the unserialized $comboarray only looks like this (var_dump): http://pastebin.com/raw.php?i=Ycwwa924
TL;DR: Pulling a serialized multidimensional array from a database, unserializing and it loses all arrays after the first one.
Any ideas what to do?
The string you've got is a serialized string plus something more at the end that is also a serialized string again and again:
a:3:{s:6:"source";s:25:"World news | The Guardian";s:8:"datetime ...
... story01.htm";}}a:3:{s:6:"source";s:16:"BBC News - World";
^^^
This format is not supported by PHP unserialize, it will only unserialize the first chunk and drop everything at the end.
Instead create one array, serialize it and store that result into the database.
Alternatively you can try to recover for the moment by un-chunking the string, however in case the paste was done right, there are more issues. But on the other hand the paste obvious isn't the done fully correct.
This is my serialized data and i want to remove one of key's value.
{a:9:{s:7:"320x480";s:16:"en_1_320x480.jpg";s:7:"320x568";s:16:"en_2_320x568.jpg";s:7:"360......
If i do like this
$pcon = unserialize($body);
$pcon[$_POST['id']]='';
$body = serialize($pcon);
It will be
{s:7:"320x480";s:16:"";s:7:"320x568...
I want like this
{s:7:"320x480";N;s:7:"320x568...
It must like this
$pcon[$_POST['id']]=Null;
I'm trying to query a serialized array value in the database in wordpress, value will be stored in the table wp_postmeta, in the column meta_value.
Well, first I stored the array by using serialize() function of php.
So for example,
$postID = 1;
$arr = array(1, 2, 3);
$ser_val = serialize($arr);
update_meta_data($postID, '_customvalue', $ser_val);
The stored values is something like this
s:30:"a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}";
Then when I tried to retrieve it by performing wordpress sql query.. What I was expecting that it will be an array since it is stored as array, but after doing so, it display as string not an array.
$get_score = $wpdb->get_row("SELECT meta_value FROM wp_postmeta WHERE meta_key = '_cummulativescore'");
$scr = unserialize($get_score->meta_value);
var_dump($scr);
//output displayed
//string(30) "a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
I did check the value using is_array() function, the result is that it is not an array
Any idea on this to get the serialize value as an array?
It looks like your data was converted to a string during serialization.
The data should be stored as
a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}
instead of
s:30:"a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
s:30 means string length 30.