PHP How to change value of serialized data and write to db - php

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;

Related

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?
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

Why does my JSON array turn into an object?

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));

How can fetch serialize data without last one?

I have a serialize data with N data, and I want to select N-1 data.
Example: i:38, i:39, i:41 data. I want to show i:38 and i:39 only.
Also I want to show all data except first one.
Example: i:38, i:39, i:41 data. I want to show i:39 and i:41 only.
Any suggestion or help?
Here is my serialize data:
a:3:{
i:38;a:2{s:12:"arrival_hour";s:5:"05:30";s:14:"departure_hour";s:5:"05:40";}
i:39;a:2:{s:12:"arrival_hour";s:5:"05:45";s:14:"departure_hour";s:5:"05:00";}
i:41;a:2:{s:12:"arrival_hour";s:5:"06:10";s:14:"departure_hour";s:5:"07:35";}
}
Just unserialize the data first, then you will get the output in an array format, after that you manipulate in whatever way you want.
$data = a:3:{
i:38;a:2{s:12:"arrival_hour";s:5:"05:30";s:14:"departure_hour";s:5:"05:40";}
i:39;a:2:{s:12:"arrival_hour";s:5:"05:45";s:14:"departure_hour";s:5:"05:00";}
i:41;a:2:{s:12:"arrival_hour";s:5:"06:10";s:14:"departure_hour";s:5:"07:35";}
}
$myArray = unserialize($data);
var_dump($myArray)
http://php.net/manual/en/function.unserialize.php

Serialized multidimensional stored in MySQLi does not print past first array

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.

php multiple session name or sub-session name?

my script currect have two session called
$_SESSION['mypic']
and
$_SESSION['mypicsrc']
can I combine this two to one session and sub-session?
like this:
$_SESSION['mypic']
$_SESSION['mypic']['src']
the $_SESSION global is an array that will only store strings. If you want to store an array inside a $_SESSION var you have to serialize it first
$data = array( 'src' => '' );
$_SESSION['mypic'] = serialize($data);
then to get it back out you have to deserialize
$data = deserialize($_SESSION['mypic']);
However, you should store your data in a database and then store an id or reference to that particular record in $_SESSION.
Actually, you only have one session there, with the values stored in $_SESSION.
You can change them like any other variable;
$_SESSION['mypic']['src'] = $_SESSION['mypicsrc'];

Categories