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

  • Home
  • SEARCH
  • 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 7094123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:31:28+00:00 2026-05-28T08:31:28+00:00

I am trying to sort an array sent from an XML feed. The Array

  • 0

I am trying to sort an array sent from an XML feed.

The Array looks like this from print_r($answer);:

Array
(
[size] => Array
    (
        [0] => 1.5m x 1.5m
        [1] => 1.5m x 3m
        [2] => 3m x 6.0m
        [3] => 3m x 2.3m
    )

[rate] => Array
    (
        [0] => 80
        [1] => 135
        [2] => 295
        [3] => 180
    )

[sortorder] => Array
    (
        [0] => 3
        [1] => 4
        [2] => 1
        [3] => 2
    )
.
.
.
)

I want to get out the array:

    Array
(
[size] => Array
    (
        [0] => 3m x 6.0
        [1] => 3m x 2.3m
        [2] => 1.5m x 1.5m
        [3] => 1.5m x 3m
    )

[rate] => Array
    (
        [0] => 295
        [1] => 180
        [2] => 80
        [3] => 135
    )

[sortorder] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
    )
.
.
.
)

What I am trying to do is use the Sort Order sub array to display the items in that order

I have tried a number of uasort() and array_multisort() examples, but all seem to order the sub arrays and not the values inside the sub arrays

Any ideas will be a great help. Cheers

  • 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-28T08:31:29+00:00Added an answer on May 28, 2026 at 8:31 am

    This one sorts $answer['sortorder'] and uses those keys to sort the rest of $answer without restructuring first.

    // sort $answer['sortorder'] and retrieve indices.
    asort($answer['sortorder']);
    $idx = array_keys($answer['sortorder']); 
    
    // do sorting
    $sorted = array();
    foreach($answer as $key=>$subarr) {
        if ($key != 'sortorder') { // don't sort
        foreach($idx as $i) {
            $sorted[$key][] = $subarr[$i];
        }
        } else {
            // $answer['sortorder'] is already sorted.
            $sorted[$key] = $subarr;
        }
    }
    print_r($sorted);
    

    See it in action here.

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

Sidebar

Related Questions

I'm trying to sort an array of people objects. The class looks like this.
I'm trying to sort an array that looks like this: var dateGroups = [
I'm trying to sort an array that would look something like this: (please ignore
I am trying to sort char array like this: #include<algorithm> ... char x[] =
Trying to sort this array of objects according to (1) depth and (2) weight,
my array jokesarray has elements like the following. I am trying to sort by
I have an array that looks something like this: Array ( [Erik] => Array
Trying to sort an array in PHP that is being populated from a CSV.
I am trying to sort an array of prices from low to high. I
I'm trying to sort an array made of random numbers from 1 to 10

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.