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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:56:12+00:00 2026-06-13T09:56:12+00:00

I have two arrays that look like this: (this one is ordered by value_max

  • 0

I have two arrays that look like this:

(this one is ordered by value_max)

$max_values = [
    ["name" => "john", "id" => 5, "value_max" => 500], 
    ["name" => "john", "id" => 3, "value_max" => 200], 
    ...
];

$min_values = [
    ["name" => "john", "id" => 5, "value_min" => 100], 
    ["name" => "john", "id" => 3, "value_min" => 150], 
    ...
];

And I need to have a final array like this:

(This one stills need to be ordered by value_max, so I assume I could just overwrite the first array with the calculations done with the second)

$max_and_difference_values = [
    ["name" => "john", "id" => 5, "value_max" => 500, "difference_value" => 400],
    ["name" => "john", "id" => 3, "value_max" => 200, "difference_value" => 50 ], 
    ...
];

My question is pretty straight: What is the best/effective way to run through both first arrays and build the last one. Assuming that the size of the arrays can be of around 150 elements.

  • 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-13T09:56:13+00:00Added an answer on June 13, 2026 at 9:56 am

    To avoid looping through all arrays repeatedly, index one array by the field you want to merge on, i.e. the 'id' key:

    $second = array_combine(array_map(function ($i) { return $i['id']; }, $second_array), $second_array);
    

    Then looping through the other and comparing the values is pretty easy:

    $third = array();
    foreach ($first_array as $i) {
        $third[] = $i + array('difference_value' => $i['value_max'] - $second[$i['id']]['value_min']);
    }
    

    If it’s guaranteed that both arrays will have exactly matching keys, you don’t even need the first step and just go by already existing keys.

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

Sidebar

Related Questions

I currently have two arrays that look like this: Swatches: Array ( [0] =>
I have two arrays that I create like this: public int GameBoard[][] = new
Let's say I have two classes that look like this: public class ByteFilter {
I have a string that looks like this: [2005] one two three [2004] six
I have 2 arrays that look like this: $array1 = [ ['id' => '434b5g6',
I have two arrays they look like $a1 = array( array('num' => 1, 'name'
I have two arrays that I would like to compare and ultimately wind up
I have two arrays of data that I'm trying to amalgamate. One contains actual
Not sure how to go about this... But, I have two arrays, one with
So in javascript I have an array that looks about like this: [{x1:0,x2:2000,y:300},{x1:50,x2:250,y:500}] 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.