Asked  7 Months ago    Answers:  5   Viewed   67 times

i need to unserialize a string to an array.Here is the stringwhich needs to be unserialized in php to assosiative array.

a:1:{i:0;s:158:"a:6:{s:5:"rowid";s:32:"94ca9ee0c4e3184b50e89e82f80332fb";s:2:"id";
s:2:"68";s:3:"qty";s:1:"1";s:5:"price";
s:2:"20";s:4:"name";
s:5:"Bread";s:8:"subtotal";i:20;}";

}

 Answers

44

It looks like this has been doubly serialized. It expands to an array with a single element, and that element is a serialized associative array. So you need to do:

$temp = unserialize($data);
$result = unserialize($temp[0]);
var_dump($result);

Result:

array(6) {
  ["rowid"]=>
  string(32) "94ca9ee0c4e3184b50e89e82f80332fb"
  ["id"]=>
  string(2) "68"
  ["qty"]=>
  string(1) "1"
  ["price"]=>
  string(2) "20"
  ["name"]=>
  string(5) "Bread"
  ["subtotal"]=>
  int(20)
}

If there can be more that one element in the top-level serialized array, use array_map to unserialize all of them:

$result = array_map('unserialize', $temp);

$result will now be a 2-dimensional array.

I'm not sure why you stored your data this way. Why not just serialize the original 2-d array all at once, instead of nesting them?

Saturday, May 29, 2021
 
jsuggs
answered 7 Months ago
40

A number like 6.2 can't be represented exactly using floating-point math in computers as there is no finite base-2 representation of it. What you are seeing when echo-ing the number is something intended for human reading, and thus the value will be rounded to what floats can provide in accuracy (about 6 decimal places for 32-bit and 17 for 64-bit FP values).

When serializing those values, however, you really want the exact value (i. e. all bits that are in there) and not just the nearest "nice" value. There could be more than one float/double representation which evaluates to approximately 6.2 and when serializing you usually really want to store he exact values to the last bit you are having in order to restore them correctly. That's why you're getting ridiculous "accuracy" in values there. It's all just to preserve the exact bit representation of what you started with.

But why exactly do you want to control the serialized output that tightly? I mean, it's just there so you can round-trip your data structure and read it back in later. You certainly don't want to use that serialized representation somewhere in output for humans or so. So if it's just about "nice-looking" values, you shouldn't use serialize which has an entirely different purpose.

Wednesday, March 31, 2021
 
dmp
answered 9 Months ago
dmp
50

If you are getting multiple results, you can loop through them like this:

$getinfo = "select user_firstname, user_middlename, user_lastname from tempuserinfo where user_email ='$ead' and activation_code='$eac'";
$query = mysql_query($getinfo, $db);

while ($row = mysql_fetch_array($query)) {
    $firstname = $row['user_firstname'];
    $lastname = $row['user_lastname'];
}

If you are only getting one row back (make sure you add a LIMIT to your SQL statement), then you can use something like this:

$getinfo = "select user_firstname, user_middlename, user_lastname from tempuserinfo where user_email ='$ead' and activation_code='$eac'";
$query = mysql_query($getinfo, $db);

$row = mysql_fetch_array($query);

$firstname = $row['user_firstname'];
$lastname = $row['user_lastname'];
Wednesday, March 31, 2021
 
Wookai
answered 9 Months ago
72

After some debugging I found the solution myself.

The problem is, when User Entity was implementing the UserInterface, the user provider(actually the Doctrine, behind the scene) tried to Serializing the User object to store it in the session but because of the file that I assigned it to this class, it fails it's career!

To solve the problem, first I tried to fetch separate User object from database but unfortunately Doctrine gave me the exact reference of the User object again.(That's not a bug. Thanks to Doctrine. It's too smart to query as less as possible).

Second, I clone the User object myself in the controller before sending it to the UserType form, and then everything went well.

But that is not the best practice because you may have some other problems with registration, profile update or other scenarios that you may have with User class.

In my application, I added another entity called Media and it stores the files with the file system and each entity like User which need some media (Like user avatar here), just have a ManyToOne relationship with this entity. In this case you can just save the name file as string in avatar field in User class.

You may have some other designs in your application but as I experienced, Do not assign a File field directly to the User entity which is implementing UserInterface!

Wednesday, March 31, 2021
 
Uours
answered 9 Months ago
35

You retrieve a serialized text from database.

You unserialize it.. change it and then when you save it back to database you need to serialize it back

//retrieve serialized data from database
$arr = unserialize($row['properties']);

//get the desired value
$khghg_id = $arr['khghg_id'];

//now insert $khghg_id in database

From the code you posted above.. I see you are unserializing 2 times.. you dont really need to do this

$cell =$row9['attachment'];
$list = unserialize($cell);
$info = $list[0]; //this line should make the difference
var_dump($info);
Friday, May 28, 2021
 
khaverim
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share