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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:10:49+00:00 2026-06-13T02:10:49+00:00

These are my arrays: $foo1 = array (5) { [0] => ‘foo1’, [1] =>

  • 0

These are my arrays:

$foo1 = array (5) {
          [0] => 'foo1',
          [1] => 'foo2',
          [2] => 'foo3',
          [3] => 'foo4',
          [4] => 'foo5'
        }

$foo2 = array (5) {
          [0] => 200,
          [1] => 130,
          [2] => 110,
          [3] => 80,
          [4] => 20
        }

$foo3 = array (5) {
          [0] => 610,
          [1] => 630,
          [2] => 500,
          [3] => 800,
          [4] => 200
        }

This is just to signify what it looks like, not my actual PHP code.

Now, what I want to do is this – I want to order the array $foo1 by the total sum of the arrays $foo2 and $foo3. But this is not the same as my last question, as this time, if the value in the array $foo2 is less than 100 or the value in the array $foo3 is less than 600, I don’t want it anywhere near the top. I want it placed behind all the last ones which have the value in the array $foo2 as above 100, and the value in the array $foo3 as above 600. So in the case of the following array, the result would be:

$foo1 = array (5) {
          [0] => 'foo1',
          [1] => 'foo2',
          [2] => 'foo4',
          [3] => 'foo3',
          [4] => 'foo5'
        }

If you didn’t fully understand above, this is why. Because the value in the array $foo2 with the same key as foo1 in the array $foo1 is above 100, and the value in the array $foo3 with the same key as foo1 in the array $foo1 is above 600, and it has the highest sum altogether. foo2 is the next one, because it is the next element which has the same index keys which fulfil the requirements, and have the highest sum. foo4 is next, because none of the other rows fulfil the requirements, and foo4‘s equivalent keys in the other arrays have the largest sum out of the rows which don’t fulfil the requirements. foo3 and foo5 are 4th and 5th for the same reason.

Hope I’m not being too specific, and any help?

  • 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-06-13T02:10:50+00:00Added an answer on June 13, 2026 at 2:10 am

    Combine them first, add a flag ("tail"=>true/false), and sort it.

    function special_sort($arr1,$arr2,$arr3)
    {
        $arr=array();
        foreach($arr1 as $key=>$val)
        {
            $arr[$key]=array("val"=>$val,"sum"=>($arr2[$key]+$arr3[$key]),"tail"=>(($arr2[$key]<100 || $arr3[$key]<600)?true:false));
        }
        usort($arr,function($a,$b){
            if($a["tail"] && !$b["tail"])
                return 1;
            elseif(!$a["tail"] && $b["tail"])
                return -1;
            elseif($a["sum"]>$b["sum"])
                return -1;
            elseif($a["sum"]<$b["sum"])
                return 1;
            else
                return 0;
        });
        return $arr;
    }
    

    Test result:

    $foo1=array("foo1","foo2","foo3","foo4","foo5");
    $foo2=array(200,130,110,80,20);
    $foo3=array(610,630,500,800,200);
    print_r(special_sort($foo1,$foo2,$foo3));
    

    Outputs

    Array
    (
        [0] => Array
            (
                [val] => foo1
                [sum] => 810
                [tail] => 
            )
    
        [1] => Array
            (
                [val] => foo2
                [sum] => 760
                [tail] => 
            )
    
        [2] => Array
            (
                [val] => foo4
                [sum] => 880
                [tail] => 1
            )
    
        [3] => Array
            (
                [val] => foo3
                [sum] => 610
                [tail] => 1
            )
    
        [4] => Array
            (
                [val] => foo5
                [sum] => 220
                [tail] => 1
            )
    
    )
    

    You’ll have to take out the “val” value if you want. That shouldn’t be hard.

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

Sidebar

Related Questions

I'm trying to merge these 2 arrays $arr1 = array('a' => 1, 'b' =>
I have 4 string arrays,each of these array's length is 4. I also have
we have these arrays.... $cities = array(nagpur,kanpur,delhi,chd,Noida,mumbai,nagpur); $names = array(munish,imteyaz,ram,shyam,ankit,Rahul,mohan); now i want a
For example, if these were my arrays: array (4) { [0] => 6, [1]
We have two unsorted arrays and each array has a length of n. These
I have these arrays: $array1 Array ( [0] => Array ( [state] => AE
I have a multidimensional array within these arrays contains array objects. How can I
I have three arrays and I have copied all these arrays into a single
If I declare these 3 arrays int a[10][10]; int b[10][15]; int c[10][30]; For which
Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]

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.