swaggest / json-diff

JSON diff/rearrange/patch/pointer library for PHP
MIT License
220 stars 30 forks source link

fix: increment index to detect associative array #65

Closed Branlute closed 11 months ago

Branlute commented 1 year ago

Hello,

For a patch which remove elements to non associative array, we need to reindex this array. This line is good https://github.com/swaggest/json-diff/blob/f4e511708060ff7511a3743fab4aa484a062bcfb/src/JsonPointer.php#L297 But the condition for $isAssociative has an issue.

We need to increment $i to compare with $index, otherwise $i will always be equal to 0 for all iterations.

outdooracorn commented 1 year ago

We've just stumbled across this as well and came up with a similar fix. Would love to see this merged soon. Pinging @vearutop for visibility.

outdooracorn commented 1 year ago

I decided to create #67 which includes a test case and increments the counter in the body of the foreach loop as I believe that to be easier to read.