|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-07-31 06:30 UTC] to at andybutkaj dot com
I try unserialize array and everything goes nice when the array has all elements filled. When one of element is empty unserialize returns false
More specific:
PHP 4.0.6
"""""""""
a:1:{s:12:"fas";a:6:{s:10:"mkey_index";s:12:"fas";s:9:"save_type";i:1;s:6:"German";i:;s:7:"English";i:;s:6:"Slovak";i:;s:5:"Czech";i:;}}
unserialize returns array where empty elements are replaced by 0
___________________________________________________________
PHP 4.2.2
"""""""""
a:1:{s:12:"fas";a:6:{s:10:"mkey_index";s:12:"fas";s:9:"save_type";i:1;s:6:"German";i:;s:7:"English";i:;s:6:"Slovak";i:;s:5:"Czech";i:;}}
unserialize returns false (if no empty elements, unserialize returns correct array)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 01:00:01 2025 UTC |
Could verify this with PHP 4.3.0-dev 2002-07-31 on Compaq Tru64: $test_array = 'a:1:{s:12:"fas";a:6:{s:10:"mkey_index";s:12:"fas";s:9:"save_typ e";i:1;s:6:"German";i:;s:7:"English";i:;s:6:"Slovak";i:;s:5:"Czech";i:;}}'; $result_array = unserialize($test_array); print_r($result_array); Returns nothing. On the same machine this works: $array = array(1,2,3,9,7,4,5,12,"lala"=>array("test"=>1,2,3,3)); $serialized_array = serialize($array); print_r($serialized_array); $result_array = unserialize($serialized_array); print_r($array); print_r($result_array); Results as expected in: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 9 [4] => 7 [5] => 4 [6] => 5 [7] => 12 [lala] => Array ( [test] => 1 [0] => 2 [1] => 3 [2] => 3 ) ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 9 [4] => 7 [5] => 4 [6] => 5 [7] => 12 [lala] => Array ( [test] => 1 [0] => 2 [1] => 3 [2] => 3 ) ) Are you shure, your serialized Array is correct?sorry, my serialized array is not correct (i edit this array to not to be so long), your example work for me, but still some differences in work with serialized string I found this in serialized string. php 4.0.6 ...array_key;s:3:"txt" -> serialized string - work ...array_key;s:0:"" -> string withour value - work ...array_key;i:1; -> serialized integer - work ...array_key;i:; -> integer without value - work php 4.2.2 ...array_key;s:3:"txt" -> serialized string - work ...array_key;s:0:"" -> string withour value - work ...array_key;i:1; -> serialized integer - work ...array_key;i:; -> integer without value - FALSE this correspond to this unworking array (4.2.2), working (4.0.6) with integer without values: a:1:{s:3:"fas";a:6:{s:10:"mkey_index";s:3:"fas";s:9:"save_type";i:1;s: 6:"German";i:;s:7:"English";i:;s:6:"Slovak";i:;s:5:"Czech";i:;}} ___________________________________________________________ I don't know if this is bug, now. I can't create this array with declaration array( ... ). In strict rules it is not bug, it is correct work. Only backwards compatibility maybe. Thank you for solve this problem!Client side function to serialize an array [JScript]: 1 function serialize (toSerialize) 2 { 3 var fill = ''; 4 5 /** chcek argument type */ 6 var argType = typeof (toSerialize); 7 8 /** decide by type, how to serialize */ 9 if (argType == 'number') 10 return 'i:' + toSerialize + ';'; 11 else if (argType == 'boolean') 12 return 'b:' + (toSerialize == true ? '1' : '0') + ';'; 13 else if (argType == 'string') 14 { 15 /** check if string doesn't be repesented as number */ 16 if (!isNaN (toSerialize)) 17 return 'i:' + toSerialize + ';'; 18 else 19 return 's:' + toSerialize.length + ':"' + toSerialize + '";'; 20 } 21 else if (argType == 'object') 22 { 23 /** serialize object usualy array */ 24 var noItems = 0 25 for (var i in toSerialize) 26 { 27 var itemKey = serialize (i); 28 var itemValue = serialize (toSerialize [i]); 29 30 fill += itemKey + itemValue; 31 noItems = noItems + 1; 32 } 33 fill = 'a:' + noItems + ':{' + fill + '}'; 34 } 35 36 /** return serialized string */ 37 return fill; 38 } ___________________________________________________________ Making an array at client-side (JScript). if (save_array[i] != "undefined") { save_array[i] = new Array(); save_array[i]["mkey_index"] = new Array(); save_array[i]["save_type"] = new Array(); save_array[i]["integer"] = new Array(); } save_array[i]["mkey_index"] = text; // some text save_array[i]["save_type"] = integer; // 1 save_array[i]["integer"] = empty_string; // '' ___________________________________________________________ Server-side (PHP 4.0.6): JScript serialized string we can unserialize with php 4.0.6 without lost any information and array looks like this: array (0 => array("mkey_index" => "some text", "save_type" => 1, "integer" => 0), ...); So you can see this: Part of a serialized string 'i:;' is after unserialize under php406 INTEGER VALUE 0!; ___________________________________________________________ Server-side 4.2.2: Unserialize return bool(false), unserialize "crashed" on sequence 'i:;' in serialized string (correct work). ___________________________________________________________ Client side function 'patch': (JScript) Above (line 17): I changed this line this way to correct serialize in JScript: 17 return (toSerialize == '') ? 's:0:"";' : 'i:' + toSerialize + ';'; PHP422 returns an array with empty string now.