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 6190451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:35:31+00:00 2026-05-24T02:35:31+00:00

I want to sort all php arrays based on first main array. This is

  • 0

I want to sort all php arrays based on first main array.

This is the main array which I want to use to sort all other arrays:

Array (

[0] => 10
[1] => 14
[2] => 15
[3] => 20
[4] => 21
[5] => 24
[6] => 25
[7] => 28
[8] => 30
[9] => 45
[10] => 60
[11] => 90
[12] => 120
[13] => 150
[14] => 180
[15] => 210
[16] => 240
[17] => 270
[18] => 365

)

This are arrays which need to be sorted :

Array (

[0] => Array
    (
        [14] => 49.21
        [20] => 71.04
        [25] => 89.58
        [30] => 100.00
    )

[1] => Array
    (
        [180] => 412.00
        [150] => 347.00
        [120] => 285.00
        [90] => 224.00
        [60] => 165.00
        [30] => 100.00
        [14] => 47.00
    )

)

I need that final result look like this:

Array (

[0] => Array
    (
        [10] => n/a
        [14] => 49.21
        [15] => n/a
        [20] => 71.04
        [21] => n/a
        [24] => n/a
        [25] => 89.58
        [28] => n/a
        [30] => 100.00
        [45] => n/a
        [60] => n/a
        [90] => n/a
        [120] => n/a
        [150] => n/a
        [180] => n/a
        [210] => n/a
        [240] => n/a
        [270] => n/a
        [365] => n/a
    )

[1] => Array
    (
        [10] => n/a
        [14] => 71.04
        [15] => n/a
        [20] => n/a
        [21] => n/a
        [24] => n/a
        [25] => n/a
        [28] => n/a
        [30] => 100.00
        [45] => n/a
        [60] => 165.00
        [90] => 224.00
        [120] => 285.00
        [150] => 347.00
        [180] => 412.00
        [210] => n/a
        [240] => n/a
        [270] => n/a
        [365] => n/a
    )

    )

Thanks.

  • 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-24T02:35:34+00:00Added an answer on May 24, 2026 at 2:35 am

    If I understand it right from your question, you want to make both arrays of the same length and set keys not in them to "n/a".

    Variant with array_merge (Demo):

    $shorten = array(
      0 => 10,
      1 => 14,
      2 => 15,
      3 => 20,
      4 => 21,
      5 => 24,
      6 => 25,
      7 => 28,
      8 => 30,
      9 => 45,
      10 => 60,
      11 => 90,
      12 => 120,
      13 => 150,
      14 => 180,
      15 => 210,
      16 => 240,
      17 => 270,
      18 => 365,
    );
    
    $data = array(
      0 => array(
        14 => '49.21',
        20 => '71.04',
        25 => '89.58',
        30 => '100.00',
      ),
      1 => array(
        180 => '412.00',
        150 => '347.00',
        120 => '285.00',
        90 => '224.00',
        60 => '165.00',
        30 => '100.00',
        14 => '47.00',
      ),
    );
    
    // default array as the base
    $shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));
    
    foreach($data as &$array) {
        // merge to get set members
        $array = array_merge($shorten, $array);
    }
    unset($array);
    
    var_dump($data);
    

    Result:

    array(2) {
      [0]=>
      array(23) {
        [0]=>
        string(3) "n/a"
        [1]=>
        string(3) "n/a"
        [2]=>
        string(3) "n/a"
        [3]=>
        string(3) "n/a"
        [4]=>
        string(3) "n/a"
        [5]=>
        string(3) "n/a"
        [6]=>
        string(3) "n/a"
        [7]=>
        string(3) "n/a"
        [8]=>
        string(3) "n/a"
        [9]=>
        string(3) "n/a"
        [10]=>
        string(3) "n/a"
        [11]=>
        string(3) "n/a"
        [12]=>
        string(3) "n/a"
        [13]=>
        string(3) "n/a"
        [14]=>
        string(3) "n/a"
        [15]=>
        string(3) "n/a"
        [16]=>
        string(3) "n/a"
        [17]=>
        string(3) "n/a"
        [18]=>
        string(3) "n/a"
        [19]=>
        string(5) "49.21"
        [20]=>
        string(5) "71.04"
        [21]=>
        string(5) "89.58"
        [22]=>
        string(6) "100.00"
      }
      [1]=>
      array(26) {
        [0]=>
        string(3) "n/a"
        [1]=>
        string(3) "n/a"
        [2]=>
        string(3) "n/a"
        [3]=>
        string(3) "n/a"
        [4]=>
        string(3) "n/a"
        [5]=>
        string(3) "n/a"
        [6]=>
        string(3) "n/a"
        [7]=>
        string(3) "n/a"
        [8]=>
        string(3) "n/a"
        [9]=>
        string(3) "n/a"
        [10]=>
        string(3) "n/a"
        [11]=>
        string(3) "n/a"
        [12]=>
        string(3) "n/a"
        [13]=>
        string(3) "n/a"
        [14]=>
        string(3) "n/a"
        [15]=>
        string(3) "n/a"
        [16]=>
        string(3) "n/a"
        [17]=>
        string(3) "n/a"
        [18]=>
        string(3) "n/a"
        [19]=>
        string(6) "412.00"
        [20]=>
        string(6) "347.00"
        [21]=>
        string(6) "285.00"
        [22]=>
        string(6) "224.00"
        [23]=>
        string(6) "165.00"
        [24]=>
        string(6) "100.00"
        [25]=>
        string(5) "47.00"
      }
    }
    

    Variant with mapping function (Demo):

    $shorten = array(
      0 => 10,
      1 => 14,
      2 => 15,
      3 => 20,
      4 => 21,
      5 => 24,
      6 => 25,
      7 => 28,
      8 => 30,
      9 => 45,
      10 => 60,
      11 => 90,
      12 => 120,
      13 => 150,
      14 => 180,
      15 => 210,
      16 => 240,
      17 => 270,
      18 => 365,
    );
    
    // overload $shorten array with a mapping function
    $shorten = function(array $a) use ($shorten)
    {
        $r = array();
        foreach($shorten as $i => $k)
        {
            $r[$k] = isset($a[$k]) ? $a[$k] : 'n/a';
        }
        return $r;
    };
    
    
    $data = array(
      0 => array(
        14 => '49.21',
        20 => '71.04',
        25 => '89.58',
        30 => '100.00',
      ),
      1 => array(
        180 => '412.00',
        150 => '347.00',
        120 => '285.00',
        90 => '224.00',
        60 => '165.00',
        30 => '100.00',
        14 => '47.00',
      ),
    );
    
    // apply mapping function to $data
    $data = array_map($shorten, $data);
    
    var_dump($data); # result
    

    Result:

    array(2) {
      [0]=>
      array(19) {
        [10]=>
        string(3) "n/a"
        [14]=>
        string(5) "49.21"
        [15]=>
        string(3) "n/a"
        [20]=>
        string(5) "71.04"
        [21]=>
        string(3) "n/a"
        [24]=>
        string(3) "n/a"
        [25]=>
        string(5) "89.58"
        [28]=>
        string(3) "n/a"
        [30]=>
        string(6) "100.00"
        [45]=>
        string(3) "n/a"
        [60]=>
        string(3) "n/a"
        [90]=>
        string(3) "n/a"
        [120]=>
        string(3) "n/a"
        [150]=>
        string(3) "n/a"
        [180]=>
        string(3) "n/a"
        [210]=>
        string(3) "n/a"
        [240]=>
        string(3) "n/a"
        [270]=>
        string(3) "n/a"
        [365]=>
        string(3) "n/a"
      }
      [1]=>
      array(19) {
        [10]=>
        string(3) "n/a"
        [14]=>
        string(5) "47.00"
        [15]=>
        string(3) "n/a"
        [20]=>
        string(3) "n/a"
        [21]=>
        string(3) "n/a"
        [24]=>
        string(3) "n/a"
        [25]=>
        string(3) "n/a"
        [28]=>
        string(3) "n/a"
        [30]=>
        string(6) "100.00"
        [45]=>
        string(3) "n/a"
        [60]=>
        string(6) "165.00"
        [90]=>
        string(6) "224.00"
        [120]=>
        string(6) "285.00"
        [150]=>
        string(6) "347.00"
        [180]=>
        string(6) "412.00"
        [210]=>
        string(3) "n/a"
        [240]=>
        string(3) "n/a"
        [270]=>
        string(3) "n/a"
        [365]=>
        string(3) "n/a"
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of objects which I want to sort based on one
I want to sort array A based on values in Array B actually in
I want to sort results set first based on one column and then based
I am having problem trying to sort an array - I want all the
I want to sort values of an array in alphabetical order in PHP. If
I have several std::vector , all of the same length. I want to sort
i want to sort dictionary based upon value of each dictioanry item.but if i
I want to sort a flat, associative array by its values alphabetically and preserve
I want to sort a list of strings based on the string length. I
I have List I want to sort Desc by Priority, which is int and

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.