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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:02:10+00:00 2026-05-25T14:02:10+00:00

I have two array like this and want to merge like Take 1st index

  • 0

I have two array like this and want to merge like

Take 1st index Array 1 check

  • if it is present in Array 2
    Insert in Array 3
    add weight of Array 1 and Array 2
  • else if not present
    take 1st index of Array 2

Take 1st index of Array 2 and check

  • if it is present in Array 1
    Insert in Array 3
    add weight of Array 1 and Array 2
  • else if not present
    take 2nd index of Array 1

Take 2nd index of Array 1 and check

  • if it is present in Array 2
    Insert in Array 3
    add weight of Array 1 and Array 2
  • else if not present
    take 1st index of Array 2

Take 1st index of Array 2 and check

Note above logic is for given example
For Example

Array 1
(
    [144] => Array
    (
        [weight] => 2
    )
    [145] => Array
    (
        [weight] => 1
    )
    [177] => Array
    (
        [weight] => 1
    )
)

Array 2
(
    [93] => Array
    (
        [weight] => 4
    )
    [133] => Array
    (
        [weight] => 4
    )
    [144] => Array
    (
        [weight] => 4
    )
    [145] => Array
    (
        [weight] => 4
    )
    [141] => Array
    (
        [weight] => 1
    )
)

I want result as

Array 3
(
    [144] => Array
    (
        [weight] => 6
    )
    [145] => Array
    (
        [weight] => 5
    )
    [93] => Array
    (
        [weight] => 4
    )
    [133] => Array
    (
        [weight] => 4
    )
    [177] => Array
    (
        [weight] => 1
    )
    [141] => Array
    (
        [weight] => 1
    )
)

Thanks in Advance

  • 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-25T14:02:11+00:00Added an answer on May 25, 2026 at 2:02 pm

    You seems to want second array first,
    then append first array

    maybe this is works

    $a = ...; //first array
    $b = ...; // second array
    
    var_export($a);
    var_export($b);
    
    $rtn = $tmp = array();
    foreach ($b as $idx=>$arr)
    {
      if (isset($a[$idx]))
      {
        $rtn[$idx]['weight'] = $arr['weight'] + $a[$idx]['weight'];
        unset($a[$idx]);
      }
      else
      {
        $tmp[$idx]['weight'] = $arr['weight'];
      }
    }
    
    $res = $rtn+$tmp+$a;
    print_r($res);
    

    result

    array (
      144 =>
      array (
        'weight' => 2,
      ),
      145 =>
      array (
        'weight' => 1,
      ),
      177 =>
      array (
        'weight' => 1,
      ),
    )array (
      93 =>
      array (
        'weight' => 4,
      ),
      133 =>
      array (
        'weight' => 4,
      ),
      144 =>
      array (
        'weight' => 4,
      ),
      145 =>
      array (
        'weight' => 4,
      ),
      141 =>
      array (
        'weight' => 1,
      ),
    )Array
    (
        [144] => Array
            (
                [weight] => 6
            )
    
        [145] => Array
            (
                [weight] => 5
            )
    
        [93] => Array
            (
                [weight] => 4
            )
    
        [133] => Array
            (
                [weight] => 4
            )
    
        [141] => Array
            (
                [weight] => 1
            )
    
        [177] => Array
            (
                [weight] => 1
            )
    
    )
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have two arrays like this first array Array ( [0228] => Array (
I have converted an array to object data like this: <?php $myobject->data = (object)Array('zero','one','two');
I have two arrays that are structured like this $array1 = Array ( [0]
So basically I have these two arrays I want to merge... array(1) { [first]=>
I have an array like this: Array ( [0] => <one@one.com> [1] => <two@two.co.in>
So I have the two array of size 256 like this: int arrayOne[] =
I have two arrays that I create like this: public int GameBoard[][] = new
Suppose I have two arrays like the following: $arr2 = array(array(first, second), array(third, fourth));
I have two arrays. I'd like to merge them into one, but remove the
I have two array and three button in segment control i want that when

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.