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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:10:08+00:00 2026-06-03T05:10:08+00:00

I have many arrays containing an ID as the primary key, with multidimensional information

  • 0

I have many arrays containing an ID as the primary key, with multidimensional information under each id. Here are two examples:

First Example Array:

array
  14181 => 
    array
      'industries' => 
        array
          'weight' => string '105652' (length=6)
          'count' => string '11' (length=2)
  48354 => 
    array
      'industries' => 
        array
          'weight' => string '508866' (length=6)
          'count' => string '10' (length=2)

Second Example Array:

array
  16434 => 
    array
      'business_types' => 
        array
          'weight' => string '104614' (length=6)
          'count' => string '1' (length=1)
  48354 => 
    array
      'business_types' => 
        array
          'weight' => string '103610' (length=6)
          'count' => string '10' (length=2)

I’d like to get the intersection of many arrays like these ( based on the key ), but I need to preserve the weight and count data from each array for each key. Notice it’s different weight and count data from each array. In this case, business_type and industries.

Final Array Needed:

array
  48354 => 
    array
      'business_types' => 
        array
          'weight' => string '103610' (length=6)
          'count' => string '10' (length=2)
      'industries' => 
        array
          'weight' => string '508866' (length=6)
          'count' => string '10' (length=2)

Originally I was not trying to keep the weights and counts, so I was simply performing an array_intersect_keys() and the job was done. Now I need to keep this data. I named the sub arrays different things in the hope that array_intersect_keys() would preserve it, however, it only preserves it for the first array in the function.

Is there a preferred way to do something like this?

The only solution I can come up with is to reduce all the arrays to a list of final ID’s ( keys ) and then loop through that array pulling the weight and count info from each of the original arrays we compared.

  • 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-03T05:10:10+00:00Added an answer on June 3, 2026 at 5:10 am

    Your proposed solution seems fine, but you might consider merging one list into the other, e.g.

    <?php
    
    $a1 = array(14181 => array('industries'     => array('weight' => "105652", 'count' => "11")),
                48354 => array('industries'     => array('weight' => "508866", 'count' => "10")));
    $a2 = array(16434 => array('business_types' => array('weight' => "104614", 'count' => "1")),
                48354 => array('business_types' => array('weight' => "103610", 'count' => "10")));
    
    //print_r($a1);
    //print_r($a2);
    
    foreach($a2 as $a2k => $a2v)
    {
        // The loop below should only go through once, but if there are multiple types it will be ok
        foreach($a2v as $a2vk => $a2vv)
        {
            $a1[$a2k][$a2vk] = $a2vv;
        }
    }
    
    printf("Output:\n");
    print_r($a1);
    
    printf("Output:\n");
    print_r($a1);
    

    Output:

    Output:
    Array
    (
        [14181] => Array
            (
                [industries] => Array
                    (
                        [weight] => 105652
                        [count] => 11
                    )
    
            )
    
        [48354] => Array
            (
                [industries] => Array
                    (
                        [weight] => 508866
                        [count] => 10
                    )
    
                [business_types] => Array
                    (
                        [weight] => 103610
                        [count] => 10
                    )
    
            )
    
        [16434] => Array
            (
                [business_types] => Array
                    (
                        [weight] => 104614
                        [count] => 1
                    )
    
            )
    
    )
    

    I believe that this will be slightly faster than your proposed solution.


    Edit: The above was an array merge type algorithm that would merge the arrays even if a key wasn’t common among the arrays. The algorithm below will only produce the intersection of the two arrays. My fault in initially getting the question:

    <?php
    
    $a1 = array(14181 => array('industries'     => array('weight' => "105652", 'count' => "11")),
                48354 => array('industries'     => array('weight' => "508866", 'count' => "10")));
    $a2 = array(16434 => array('business_types' => array('weight' => "104614", 'count' => "1")),
                48354 => array('business_types' => array('weight' => "103610", 'count' => "10")));
    
    //print_r($a1);
    //print_r($a2);
    
    // Pass the smaller array as first argument
    if(count($a1) <= count($a2))
    {
        $res = my_merge($a1, $a2);
    }
    else
    {
        $res = my_merge($a2, $a1);
    }
    
    function my_merge($a1, $a2)
    {
        $res = array();
        foreach($a1 as $a1k => $a1v)
        {
            if(array_key_exists($a1k, $a2))
            {
                $res[$a1k] = array_merge($a1[$a1k], $a2[$a1k]);
            }
        }
        return $res;
    }
    
    printf("Output:\n");
    print_r($res);
    

    Output:

    Array
    (
        [48354] => Array
            (
                [industries] => Array
                    (
                        [weight] => 508866
                        [count] => 10
                    )
    
                [business_types] => Array
                    (
                        [weight] => 103610
                        [count] => 10
                    )
    
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many large multidimensional NP arrays (2D and 3D) used in an algorithm.
Im looking for an example of multidimensional arrays. I have an array of thumbtails
Hi I have a text file containing two arrays and one value(all integers) like
Groups has_ many users. I have 2 activerecord arrays: 1. groups = containing groups
I've got a class with many string arrays. I'd like to have one generic
-In my c code I have a struct which contains many unknown sized arrays
I'm trying to add values from array to DB, have tried many variuos examples
I have a PHP 2D array, many keys, each with one value, I need
I have many links in my page. For example <a href=/promotions/download/schools/australia.aspx>Australia</a> Now I want
I have many constant arrays that do not all have the same number of

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.