0
$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

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    Unserialize them, combine them however you want, then serialize that. – Barmar Nov 07 '17 at 17:43
  • `i` stands for `integer`, `s` stands for `string`. – Barmar Nov 07 '17 at 17:44
  • Are you asking how to do this? If so, why are you judging anything by its serialized output, rather than just specifying the array you want. – iainn Nov 07 '17 at 17:44
  • And `a` stands for `array`. – Barmar Nov 07 '17 at 17:44
  • See https://stackoverflow.com/questions/14297926/structure-of-a-serialized-php-string to understand the parts of a serialized string. – Barmar Nov 07 '17 at 17:45
  • @Barmar They're right aren't they? Arrays are serialized as `a:count:{key;value}` – iainn Nov 07 '17 at 17:49
  • @Barmar can you please provide me the code how to do that, 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 – Nitesh kumar Nov 07 '17 at 17:52

2 Answers2

2

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.

DEMO

For the meaning of s, see Structure of a Serialized PHP string

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • a:2:{i:0;s:3:"123";i:1;s:2:"56";} as you can see the i:0 and i:1 but is should be same like a:2:{i:123;s:3:"123";i:56;s:2:"56";} (i:123 and i:56) – Nitesh kumar Nov 07 '17 at 17:58
  • `array_merge` reindexes the arrays if the keys are numbers rather than strings. That's why we use `+` in the answers. – Barmar Nov 07 '17 at 18:00
  • one last thing please help $var1='a:1:{i:4;i:4;}'; $var2='a:1:{i:3;s:1:"3";}'; output = a:2:{i:3;s:1:"3"; i:4;s:1:"4";} as you can see it add s:1 to $var1. how can i do that – – Nitesh kumar Nov 07 '17 at 18:17
  • $tournament_candidate='a:1:{i:4;i:4;}' and $tournament_competitors='a:1:{i:3;s:1:"3";}'; after union it should be like this a:2:{i:3;s:1:"3"; i:4;s:1:"4";} – Nitesh kumar Nov 07 '17 at 18:23
  • Why is the value a number instead of a string in the first array? – Barmar Nov 07 '17 at 18:24
  • After unserializing, you can use a `foreach` loop to replace numbers with strings. – Barmar Nov 07 '17 at 18:25
  • But most of the time PHP doesn't care, it will automatically convert between numbers and strings as needed. – Barmar Nov 07 '17 at 18:25
  • 1
    Stop thinking in terms of serialized data, and work with the original arrays. – Barmar Nov 07 '17 at 18:26
  • Thank you for this! – Karra Max Nov 06 '20 at 23:32
0

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.

iainn
  • 16,826
  • 9
  • 33
  • 40
  • one last thing please help $var1='a:1:{i:4;i:4;}'; $var2='a:1:{i:3;s:1:"3";}'; output = a:2:{i:3;s:1:"3"; i:4;s:1:"4";} as you can see it add s:1 to $var1. how can i do that – Nitesh kumar Nov 07 '17 at 18:17