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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:12:41+00:00 2026-06-08T20:12:41+00:00

I wrote the following code to take arrays in PHP and join them together

  • 0

I wrote the following code to take arrays in PHP and “join” them together the way that a LEFT JOIN would in MySQL. For my sake I wrote the function using foreach and passed the arrays in and returned a new array… I feel like it’s pretty readable, but I also know that it’s inefficient.

Ideally, I’d like this function to handle upwards of 10,000 rows per array, and I suspect that means: 1) passing $original by reference (to save memory), and 2) using one of PHP iterative array function rather than a foreach (to save processing time).

Usually I don’t post these sort of “quiz” like questions, but I feel like the answer to this will benefit the community. (Like this guy: Join 2 multidimensional array)

May the 1) fastest, and 2) lowest memory consuming answer win! 😛

<?php

// Join Arrays on Keys (**updated with knittl's suggestion**)
function array_join($original, $merge, $on) {
    if (!is_array($on)) $on = array($on);
    foreach ($merge as $remove => $right) {
        foreach ($original as $index => $left) {
            foreach ($on as $from_key => $to_key) {
                if (!isset($original[$index][$from_key])
                    || !isset($right[$to_key])
                    || $original[$index][$from_key] != $right[$to_key])
                    continue 2;
            }
            $original[$index] = array_merge($left, $right);
            unset($merge[$remove]);
        }
    }
    return array_merge($original, $merge);
}

// Test Arrays
$data1 = array(
    array(
        'productId' => '822335',
        'dateHour' => '2011-11-17 06:00:00',
        'qtySold' => '200',
        'qtyCanceled' => '10',
    ),
    array(
        'productId' => '822335',
        'dateHour' => '2011-11-17 07:00:00',
        'qtySold' => '100',
        'qtyCanceled' => '20',
    ),
    array(
        'productId' => '822336',
        'dateHour' => '2011-11-17 06:00:00',
        'qtySold' => '0',
        'qtyCanceled' => '30',
    ),
    array(
        'productId' => '822336',
        'dateHour' => '2011-11-17 07:00:00',
        'qtySold' => '50',
        'qtyCanceled' => '40',
    ),
);

$data2 = array(
    array(
        'entity_id' => '822335',
        'dateHour' => '2011-11-17 06:00:00',
        'productInventory' => '300',
    ),
    array(
        'entity_id' => '822335',
        'dateHour' => '2011-11-17 07:00:00',
        'productInventory' => '200',
    ),
    array(
        'entity_id' => '822336',
        'dateHour' => '2011-11-17 06:00:00',
        'productInventory' => '100',
    ),
    array(
        'entity_id' => '822336',
        'dateHour' => '2011-11-17 07:00:00',
        'productInventory' => '50',
    ),
);

// Usage
$result = array_join($data1, $data2, array(
    'productId' => 'entity_id',
    'dateHour' => 'dateHour'
));
print_r($result);
  • 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-08T20:12:42+00:00Added an answer on June 8, 2026 at 8:12 pm

    Better?

    function array_join($original, $merge, $on) {
        if (!is_array($on)) $on = array($on);
        foreach ($merge as $remove => $right) {
            foreach ($original as $index => $left) {
                foreach ($on as $from_key => $to_key) {
                    if (!isset($original[$index][$from_key])
                    || !isset($right[$to_key])
                    || $original[$index][$from_key] != $right[$to_key])
                        continue 2;
                }
                $original[$index] = array_merge($left, $right);
                unset($merge[$remove]);
            }
        }
        return array_merge($original, $merge);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, the idea is that I would take the following code used to run
I was so sleepy that I wrote the following code (modified to just show
i wrote following code to create a linkbutton programmatically, but its showing like lable
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
I wrote the following code for text file encryption and decryption. The encryption process
I wrote the following code to select text from database,but when i echo the
if wrote the following code and do not understand why the trace returns false:
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
I wrote the following code snippet in VS2010: #pragma once #include stdafx.h #ifndef SECURITY_WIN32
I wrote the following code to count the node of an XML file: private

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.