doddsey_65 Posted December 30, 2011 Share Posted December 30, 2011 I have an array from the database like so Array (2) ( | ['0'] => Array (4) | ( | | ['f_fid'] = String(1) "1" | | ['0'] = String(1) "1" | | ['f_names'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} " | | ['1'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} " | ) | ['1'] => Array (4) | ( | | ['f_fid'] = String(1) "2" | | ['0'] = String(1) "2" | | ['f_names'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} " | | ['1'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} " | ) ) So i use a foreach loop to turn the serialized value into an array foreach($fields as $key => $val) { $fields[$key]['f_names'] = unserialize($val['f_names']); } But for some reason the last f_names is false Array (2) ( | ['0'] => Array (4) | ( | | ['f_fid'] = String(1) "1" | | ['0'] = String(1) "1" | | ['f_names'] => Array (4) | | ( | | | ['0'] = String(18) "register_user_name" | | | ['1'] = String(5) "r_u_n" | | | ['2'] = String(10) "user_alias" | | | ['3'] = String(12) "nickusername" | | ) | | ['1'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} " | ) | ['1'] => Array (4) | ( | | ['f_fid'] = String(1) "2" | | ['0'] = String(1) "2" | | ['f_names'] = Boolean(0) FALSE | | ['1'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} " | ) ) Anyone have any idea why? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/254080-weird-unserialize-problem/ Share on other sites More sharing options...
Muddy_Funster Posted December 30, 2011 Share Posted December 30, 2011 because you are only telling it to deserialise $fields[$key]['f_names](value list), the field that is remaining serialised is $fields[$key]['1'](value list). add another foreach to handle that and you should be rockin' Quote Link to comment https://forums.phpfreaks.com/topic/254080-weird-unserialize-problem/#findComment-1302556 Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2011 Share Posted December 30, 2011 While it doesn't have anything to do with your problem, you should be using a fetch_assoc statement to retrieve your data (you are using fetch_array now, giving both associative and numerical arrays in your data.) If that's your actual data, it is invalid for the 2nd set of values (the length of some of the strings doesn't match what the serialized syntax says they are.) What does using var_export on the data produce (so that someone here could reproduce the problem.) Edit: Specifically, the strings you show in that data would produce the following serialized output - a:4:{i:0;s:17:"register_password";i:1;s:11:"r_pass_word";i:2;s:9:"user_pass";i:3;s:13:"reg-pass-word";} Quote Link to comment https://forums.phpfreaks.com/topic/254080-weird-unserialize-problem/#findComment-1302560 Share on other sites More sharing options...
doddsey_65 Posted December 30, 2011 Author Share Posted December 30, 2011 heres the var_export dump array ( 0 => array ( 'f_fid' => '1', 0 => '1', 'f_names' => 'a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} ', 1 => 'a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} ', ), 1 => array ( 'f_fid' => '2', 0 => '2', 'f_names' => 'a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} ', 1 => 'a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} ', ), ) Im using PDO's fetch_results. I used to use fetch_assoc within a while loop but thought that a foreach loop would work better. PDO's fetch_assoc only brings back one row if not used within a while loop. Quote Link to comment https://forums.phpfreaks.com/topic/254080-weird-unserialize-problem/#findComment-1302562 Share on other sites More sharing options...
ialsoagree Posted December 30, 2011 Share Posted December 30, 2011 I'm not sure what's wrong, but wanted to point this out in case it means something to anyone else, I think it's what PFMaBiSmAd was refering to. This is from the first index: ['f_names'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} " ['1'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} " This is from the second index: ['f_names'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} " ['1'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} " Notice that in the first index, f_names has "register_user_name" "r_u_n" "user_alias" etc. In the second index, f_names is an EXACT duplicate of '1' from below, it doesn't have "register_user_name" "r_u_n" or "user_alias". Quote Link to comment https://forums.phpfreaks.com/topic/254080-weird-unserialize-problem/#findComment-1302563 Share on other sites More sharing options...
doddsey_65 Posted December 30, 2011 Author Share Posted December 30, 2011 even when using fetch_assoc it still brings up the same case. while($fields = $sth->fetch(PDO::FETCH_ASSOC)) { $names[] = unserialize($fields['f_names']); } a dump of $fields in the loop shows Array (2) ( | ['f_fid'] = String(1) "1" | ['f_names'] = String(99) "a:4:{i:0;s:18:"register_user_name";i:1;s:5:"r_u_n";i:2;s:10:"user_alias";i:3;s:12:"nickusername";} " ) Array (2) ( | ['f_fid'] = String(1) "2" | ['f_names'] = String(104) "a:4:{i:0;s:18:"register_password";i:1;s:5:"r_pass_word";i:2;s:10:"user_pass";i:3;s:12:"reg-pass-word";} " ) but when i dump $names all i get is Array (2) ( | ['0'] => Array (4) | ( | | ['0'] = String(18) "register_user_name" | | ['1'] = String(5) "r_u_n" | | ['2'] = String(10) "user_alias" | | ['3'] = String(12) "nickusername" | ) | ['1'] = Boolean(0) FALSE ) Quote Link to comment https://forums.phpfreaks.com/topic/254080-weird-unserialize-problem/#findComment-1302566 Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2011 Share Posted December 30, 2011 I edited my post above with what the actual serialized data should have been (your's is in valid.) Any chance you did some kind of global search/replace/update query on the data to change the f_names, without actually re-serializing the data to match? Quote Link to comment https://forums.phpfreaks.com/topic/254080-weird-unserialize-problem/#findComment-1302567 Share on other sites More sharing options...
doddsey_65 Posted December 30, 2011 Author Share Posted December 30, 2011 i thought it would be something simple. The data is stored in a blob within the database. I edited the first blob file to create the second, forgetting to change the sizes of the values. Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/254080-weird-unserialize-problem/#findComment-1302568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.