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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:09:42+00:00 2026-05-15T11:09:42+00:00

I have two sets of arrays like this for example. $Arr1[‘uid’][]=’user 1′; $Arr1[‘weight’][]=1; $Arr1[‘uid’][]=’user

  • 0

I have two sets of arrays like this for example.

$Arr1['uid'][]='user 1'; $Arr1['weight'][]=1;
$Arr1['uid'][]='user 2'; $Arr1['weight'][]=10;
$Arr1['uid'][]='user 3'; $Arr1['weight'][]=5;

$Arr2['uid'][]='user 1'; $Arr2['weight'][]=3;
$Arr2['uid'][]='user 4'; $Arr2['weight'][]=20;
$Arr2['uid'][]='user 5'; $Arr2['weight'][]=15;
$Arr2['uid'][]='user 2'; $Arr2['weight'][]=2;

The size of two arrays could be different of course. $Arr1 has coefficient of 0.7 and $Arr2 has coefficient of 0.3. I need to calculate following formula

$result=$Arr1['weight'][$index]*$Arr1Coeff+$Arr2['weight'][$index]*$Arr2Coeff;

where $Arr1['uid']=$Arr2['uid']. So when $Arr1['uid'] doesn’t exists in $Arr2 then we need to omit $Arr2 and vice versa.
And, here is an algorithm I am using now.

foreach($Arr1['uid'] as $index=>$arr1_uid){
    $pos=array_search($arr1_uid, $Arr2['uid']);
    if ($pos===false){
        $result=$Arr1['weight'][$index]*$Arr1Coeff;
        echo "<br>$arr1_uid has not found and RES=".$result;
    }else{
        $result=$Arr1['weight'][$index]*$Arr1Coeff+$Arr2['weight'][$pos]*$Arr2Coeff;
        echo "<br>$arr1_uid has found on $pos and RES=".$result;
    }
}

foreach($Arr2['uid'] as $index=>$arr2_uid){
    if (!in_array($arr2_uid, $Arr1['uid'])){
        $result=$Arr2['weight'][$index]*$Arr2Coeff;
        echo "<br>$arr2_uid has not found and RES=".$result;
    }else{
        echo "<br>$arr2_uid has found somewhere";
    }
}

The question is how can I optimize this algorithm? Can you offer other better solution for this problem?

Thank you.

  • 1 1 Answer
  • 2 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-15T11:09:42+00:00Added an answer on May 15, 2026 at 11:09 am

    Due to the way you have your arrays organized, you can use array_combine($keys, $values) to assemble $Arr1 and $Arr2 into associative arrays using keys from ['uid'] and values from ['weight']. Using the associative arrays simplifies the calculation quite a bit:

    $combi1 = array_combine($Arr1['uid'], $Arr1['weight']);
    $combi2 = array_combine($Arr2['uid'], $Arr2['weight']);
    
    // loop through the keys from both arrays
    foreach (array_keys($combi1+$combi2) as $uid) {
        // use the value from $combi1, or 0 if it isn't set
        $value1 = isset($combi1[$uid]) ? $combi1[$uid] : 0;
        // use the value from $combi2, or 0 if it isn't set
        $value2 = isset($combi2[$uid]) ? $combi2[$uid] : 0;
        // calculate our final weight
        $result = $value1 * $Arr1Coeff + $value2 * $Arr2Coeff;
        echo "<br>$uid final weight: ".$result."\n";
    }
    

    Results Compared

    Your code:

    user 1 has found on 0 and RES=1.6
    user 2 has found on 3 and RES=7.6
    user 3 has not found and RES=3.5
    user 1 has found somewhere
    user 4 has not found and RES=6
    user 5 has not found and RES=4.5
    user 2 has found somewhere

    My Code:

    user 1 final weight: 1.6
    user 2 final weight: 7.6
    user 3 final weight: 3.5
    user 4 final weight: 6
    user 5 final weight: 4.5
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two sets of data, (Ax, Ay; Bx, By). I'd like to plot
I have two arrays with two sets of values in ($a[] and $b[]) I
I have two sets of arrays. I need to get the letters that are
I have two sets of arrays with a variable number of elements, for instance:
I have two sets of elements with (sometimes) corresponding rel and id attributes: <a
I have two Sets. Set b is the subset of Set a . they're
I have two sets of vectors, set A and set B. Let's say set
Assume that we have two sets: A=(a_1,a_2,...,a_m) and B=(b_1,b_2,...,a_n) (Not necessarily of same size).
I am working on a filter functionality using ajax/jquery and php/mysql.I have two sets
I have two data sets in google app engine datastore. class First_Set(db.Model): start_time =

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.