dizews / php-ubjson

Universal Binary JSON for PHP
9 stars 7 forks source link

Fixing zero-indexed arrays #5

Closed mihai-stancu closed 9 years ago

mihai-stancu commented 9 years ago

In the original code the $key starts from 0 but is incremented inside the loop before being used.

Because of this an originally zero-indexed array would have (incorrectly) always started from an index of 1.

This patch fixes the problem.

dizews commented 9 years ago

thanks, when I wrote this code I did not want have useless IF, but your variant is more clear.

mihai-stancu commented 9 years ago

If you expect to use this code PHPEncode => UBJSON => JSDecode then there is indeed no problem because arrays in JS will be reindexed when you decode it.

But if you expect to use this code PHPEncode => UBJSON => PHPDecode then there will be differences between the original encoded value and the decoded value.

I was writing a unit test for a bundle that uses PHP Encode/Decode with UBJSON and noticed the difference that way :).