Ensure associative arrays are properly normalised #133
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'm currently working with geoPHP within Drupal to generate polygons for geographical regions, but for some reason some of the arrays that are coming into
WKB.class.php
are associative, with their index starting from1
. In these cases, when$components[] = new Point($line_coords[$i],$line_coords[$i+1]);
is called, an exception is thrown because$line_coords
has no key$i + 1
.I intend to investigate why this function is getting malformed keys, and try and correct it, but decided to put together some sort of interim remedy in case anyone else had the same problem. This PR normalises the input array so that it's no longer associative, ensuring that the keys start from
0
and are properly consecutive. It also ensures that any malformed points are dropped. (Is your policy to drop malformed points, or to try and display them by any means necessary?)Am also wondering if maybe we could useActually, I've gone ahead and done that. Requires PHP 5.3+, but the functionality in question is much more apparent now.array_chunk
to chunk the array into 2-tuples, and then clean up any malformed ones at the end.