Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6927725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:05:34+00:00 2026-05-27T11:05:34+00:00

Using PHP, I must operate on a JSON-encoded stream that was initialized in the

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:05:35+00:00Added an answer on May 27, 2026 at 11:05 am

    Assuming you have the data decoded as PHP array in $set (as associative array)

    $sets = json_decode($json_data, true);
    

    You then simply iterate over the elements of the array:

    if(isset($sets[$_GET['set']])) {
        $set = $sets[$_GET['set']];
        $result = array();
        foreach($set['index'] as $entry) {
            $result[] = $entry['id']];
        }
        echo join(',', $result);
        // or echo json_encode($result);
    }
    

    Edit: Forgot about the sorting. You could sort by the points’ Euclidean distance:

    function d($x, $y) {
        return sqrt(pow($_GET['x'] - $x, 2) + pow($_GET['y'] - $y, 2));
    }
    
    function sort_($a, $b) {
        return d($a['x'], $a['y']) - d($b['x'], $b['y']);
    }
    
    
    $set = $sets[$_GET['set']]['index'];
    usort($set, 'sort_');
    $result = array();
    foreach($set as $entry) {
        $result[] = $entry['id'];
    }
    echo join(',', $result);
    // or echo json_encode($result);
    

    DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using PHP's Image and GD functions you can use the following method to finally
I have read the following tutorial Uploading Files To the Server Using PHP and
I am using php function usort to sort an array. The custom php function
I'm using PHP include to include a PHP file that has HTML in it.
I have a set of PHP server side scripts that maintain user session state
How can I check for height and width before uploading image, using PHP. Must
I'm developping a website using php and the template.inc class. The problem is that
I'm developing a platform that allows users to create apps using PHP. Is there
Using PHP, if I set a custom session folder to store session files, what
I'm trying to reproduce a POST request that was captured from WireShark using PHP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.