Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中连接数组(如MySQL连接)_Php_Arrays_Database_Csv_Left Join - Fatal编程技术网

在PHP中连接数组(如MySQL连接)

在PHP中连接数组(如MySQL连接),php,arrays,database,csv,left-join,Php,Arrays,Database,Csv,Left Join,我想在PHP中加入两个数组,就像我在MySQL中一样joinleft。我有两个阵列: 来自数据库的$db_products(它有id和sku字段) 来自csv文件的$csv\u价格(它有sku和price字段) $db_products:(它有4000个项目) $csv\u价格:(它有8000件商品) 加入是$db_产品[$key][sku]=$csv_价格[$key][sku]。为了找到匹配对,我在循环中进行循环,结果是4000*8000检查匹配。它消耗了大量的精力和时间,我想提高效率 我

我想在PHP中加入两个数组,就像我在MySQL中一样
joinleft
。我有两个阵列:

  • 来自数据库的
    $db_products
    (它有
    id
    sku
    字段)
  • 来自csv文件的
    $csv\u价格
    (它有
    sku
    price
    字段)
$db_products
:(它有4000个项目)

$csv\u价格
:(它有8000件商品)

加入是
$db_产品[$key][sku]=$csv_价格[$key][sku]
。为了找到匹配对,我在循环中进行循环,结果是4000*8000检查匹配。它消耗了大量的精力和时间,我想提高效率


我可以通过
unset()
-ing使用的
$csv\u价格[$key]
,减少10%的使用时间,但是我希望更高效。

如果您循环使用
$csv_products
数组一次,并将SKU设置为数组键,那么您就不必在每次使用新产品时都以指数形式循环使用该数组来查找其匹配项

相反,您只需在产品数组中循环,并使用
isset()
查看它是否存在

这样,您只需重复1x
count($db\u产品)
和1x
count($csv\u价格)
次,而不是
count($db\u产品)*count($csv\u价格)
次(12000对3200万)

这个概念可以被认为类似于数据库中的索引——您使用要查找的键作为数组键/索引,这意味着您不必每次都在数组中循环以找到所需的记录


当然,在您开始使用大型数据集之前,您不会看到性能上的显著差异—随着数据集的增长,这种变化将以指数级的速度出现。

这对我来说很好

function left_join_arrays($key, $array1, $array2) {
    
    $i=0;
    foreach ($array1 as $arr1) {
        foreach ($array2 as $arr2) {
            if ($arr1[$key]==$arr2[$key]) {
                foreach(array_keys($arr2) as $key2){
                    if ($key != $key2) {
                        $array1[$i][$key2]=$arr2[$key2];
                    }
                }
                continue;
            }
        }
        $i++;
    }

    return $array1;
}

我非常高兴。其结果相同,但运行时间为0.9秒,而不是17-18秒。谢谢你,先生!:)另请参见:通用PHP实现(aod代表ArrayOfDictionary)(Dictionary代表AssociationArray)您可以在2019年看到这个答案。我已经实现了一个PHP库,用于对PHP数组进行连接、排序、分组等操作。库的名称为arraybase。
Array
(
    [0] => Array
        (
            [sku] => asd123
            [price] => 15.50
    )
    [N] => Array
    (
            [sku] => ...
            [price] => ...
    )
)
// Reindex the CSV array to use SKU as the key
$reindexed = array();
foreach ($csv_prices as $values) {
    $reindexed[$values['sku']] = $values;
}
unset($csv_prices);

// Join the prices
foreach ($db_products as &$product) {
    $sku = $product['sku'];
    $product['price'] = isset($reindexed[$sku]) ? $reindexed[$sku]['price'] : '';
}
function left_join_arrays($key, $array1, $array2) {
    
    $i=0;
    foreach ($array1 as $arr1) {
        foreach ($array2 as $arr2) {
            if ($arr1[$key]==$arr2[$key]) {
                foreach(array_keys($arr2) as $key2){
                    if ($key != $key2) {
                        $array1[$i][$key2]=$arr2[$key2];
                    }
                }
                continue;
            }
        }
        $i++;
    }

    return $array1;
}