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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:43:05+00:00 2026-05-26T09:43:05+00:00

I need to merge a three-dimensional array with a two-dimensional array based on a

  • 0

I need to merge a three-dimensional array with a two-dimensional array based on a shared value for ‘id.’

In the example below, “George Washington” has an ‘id’ of 1. I need to append his ‘value’, which is found in the second array where ‘id’ is also 1.

The ‘id’ of “John Adams” is 2, which is not found in the second array. As a result, his ‘value’ needs to be set to 0 (or NULL).

The final result is a three-dimension array where ‘value’ has been added to each item in the original array.

Array #1

Array
(
     [0] => Array
         (
             [0] => Array
                 (
                     [id] => 1
                     [name] => "George Washington"
                 )
             [total] => 8
             [average] => 2.5
         )
     [1] => Array
         (
             [0] => Array
                 (
                     [id] => 2
                     [name] => "John Adams"
                 )
             [total] => 6
             [average] => 3.0
         )
     [2] => Array
         (
             [0] => Array
                 (
                     [id] => 5
                     [name] => "James Monroe"
                 )
             [total] => 9
             [average] => 2.0
         )
)

Array #2

Array
(
     [0] => Array
         (
             [id] => 1
             [value] => 12
         )
     [1] => Array
         (
             [id] => 5
             [value] => 18
         )
)

Desired Result:

Array
(
     [0] => Array
         (
             [0] => Array
                 (
                     [id] => 1
                     [name] => "George Washington"
                 )
             [total] => 8
             [average] => 2.5
             [value] => 12
         )
     [1] => Array
         (
             [0] => Array
                 (
                     [id] => 2
                     [name] => "John Adams"
                 )
             [total] => 6
             [average] => 3.0
             [value] => 0
         )
     [2] => Array
         (
             [0] => Array
                 (
                     [id] => 5
                     [name] => "James Monroe"
                 )
             [total] => 9
             [average] => 2.0
             [value] => 18
         )
 )

What I’ve tried so far:

I separated all of the ‘id’ values from the first array into a new array named $ids. Then while looping through the items in the second array, I check to see whether the ‘id’ from the second array is found in the $ids array.

But then I’m stuck because I don’t know how to specify which item in the first array needs to receive the new ‘value’. Also, this seems like a messy solution because of the overhead involved in creating the $ids array.

  • 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-26T09:43:06+00:00Added an answer on May 26, 2026 at 9:43 am

    Didn’t even realize you had tried to list the IDs and such before I was done writing this, but here you go anyway. Good luck!

    $array1_size = count($array1);
    $array2_size = count($array2);
    
    // Creates a list containing all available IDs of Array1
    for ($i = 0; $i < $array1_size; $i ++) {
        $id_list[] = $array1[$i][0]['id'];
        $array1[$i]['value'] = NULL; // Adds a NULL to all value fields
    }
    
    // Loops through Array2
    for ($i = 0; $i < $array2_size; $i++) {
        if (in_array($array2[$i]['id'], $id_list)) { // Checks if each ID exists in the ID list
    
            $key = array_search($array2[$i]['id'], $id_list); // Gets the key of the matching ID
            $array1[$key]['value'] = $array2[$i]['value']; // Adds the value
    
        }
    }
    

    Edit: Values are now set to NULL by default and thus only gets changed later on if needed.

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

Sidebar

Related Questions

I have two lists of IP addresses. I need to merge them into three
I have two array I need to merge, and using the Union (|) operator
I need to merge two SELECT statements. There are two tables in my database,
I need to merge two XML documents, overwriting the overlapsed attributes and elements. For
I need to be able to merge two (very simple) JavaScript objects at runtime.
Say, I employ merge sort to sort an array of Integers. Now I need
I need to merge two doubly-linked lists, but not by their values (the lists
i need to merge an 2d array, if possible without any loops. Is there
I need to merge two variables. They both are unsigned ints. First: 11000000 Second:
I am using Oracle, I need to merge two queries, but i would like

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.