Using PHP, I must operate on a JSON-encoded stream that was initialized in the following manner:
set = new Array();
set[n] = {"index": new Array()};
set[n].index[i] = {"x": xLoc, "y": yLoc, "id": "id#"};
Which ultimately exports to a JSON-encoded stream whose structure looks like this:
[{"index":[{"x":156,"y":219,"id":"#1"},{"x":183,"y":229,"id":"#2"},{"x":540,"y":131,"id":"#3"},{"x":129,"y":300,"id":"#4"}]},{"index":[{"x":175,"y":214,"id":"#1"},{"x":188,"y":206,"id":"#2"},{"x":498,"y":231,"id":"#3"},{"x":150,"y":104,"id":"#4"}]},{"index":[{"x":123,"y":327,"id":"#1"},{"x":96,"y":256,"id":"#2"},{"x":12,"y":125,"id":"#3"},{"x":27,"y":32,"id":"#4"}]},{"index":[{"x":300,"y":145,"id":"#1"},{"x":104,"y":29,"id":"#2"},{"x":54,"y":11,"id":"#3"},{"x":29,"y":16,"id":"#4"}]},{"index":[{"x":11,"y":23,"id":"#1"},{"x":214,"y":16,"id":"#2"},{"x":423,"y":211,"id":"#3"},{"x":161,"y":89,"id":"#4"}]},{"index":[{"x":6,"y":202,"id":"#1"},{"x":432,"y":62,"id":"#2"},{"x":328,"y":55,"id":"#3"},{"x":93,"y":416,"id":"#4"}]}]
My query will look something like:
sort.php?set=2&x=12&y=214
From the above example, I would like to return/echo a comma-separated list of id#’s for each index of set[n] in ascending order from nearest to furthest compared to the queried x and y locations.
I am a novice at PHP and have no clue where to begin. I hope my question makes sense. Thanks in advance!
Assuming you have the data decoded as PHP array in
$set(as associative array)You then simply iterate over the elements of the array:
Edit: Forgot about the sorting. You could sort by the points’ Euclidean distance:
DEMO