$var1='a:1:{i:123;s:3:"123";}';
$var2='a:1:{i:56;s:2:"56";}';
output a:2:{i:56;s:2:"56";i:123;s:3:"123";}
Without changing the value of i
Example2;
$var1='a:2:{i:56;s:2:"56";i:123;s:3:"123";}';
$var2='a:1:{i:154;s:3:"154";}';
ouput a:3:{i:56;s:2:"56";i:123;s:3:"123";i:154;s:3:"154";}
i am using
$a=unserialize($var1);
$a2=unserialize($var2);
$result = array_merge($a, $a2);
$serialized_array=serialize($result);
print_r($serialized_array);
but all the values of i got changed
also what does s
stands for in above strings
Unserialize them, concatenate the arrays, then serialize that.
echo serialize(unserialize($var1) + unserialize($var2));
You have to use +
instead of array_merge()
because the latter re-indexes the array if the keys are all integers. Since all your keys begin with i:
, that means they're numeric indexes.
For the meaning of s
, see Structure of a Serialized PHP string
Using array_merge
will re-index arrays with numeric keys. If you want to avoid this, you can use the array union operator (+
) instead:
$combined = unserialize($var2) + unserialize($var1);
This will give you the correct serialized output.
See https://eval.in/894864 to demonstrate the difference.