dongzhiyan5693 2012-08-01 17:17
浏览 145
已采纳

如何优化我的array_join(模拟LEFT JOIN)函数?

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! :P

<?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条回答 默认 最新

  • douyan1244 2012-08-01 17:34
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看