【问题标题】:PHP Array function to compare and merge values用于比较和合并值的 PHP Array 函数
【发布时间】:2020-04-24 18:07:54
【问题描述】:

珍惜你的时间!

在查看了几个“比较和合并”线程之后,最后,我将请求某人帮助解决这个非常具体的场景。

$input = array(
  [ 2616 ] => array(
    [ 9878767654 ] => array(
      [ 987987987 ] => 987987987, 
      [ 987987986 ] => 987987986,
    ),
  ), 
  [ 2618 ] => array(
    [ 9878767654 ] => array(
      [ 987987987 ] => 987987987,
    ),

  ), 
  [ 'tmp-9878767654' ] => array(
    [ 9878767654 ] => array(
      [ 987987985 ] => 987987985, 
      [ 987987987 ] => 987987987,
    ),

  ), 
  [ 'tmp-9878767655' ] => array(
    [ 9878767655 ] => array(
      [ 987987975 ] => 987987975,
    ),
  ),
);

$desired_output = array(
  [ 2616 ] => array(
    [ 9878767654 ] => array(
      [ 987987987 ] => 987987987, 
      [ 987987986 ] => 987987986,
      [ 987987985 ] => 987987985,
    ),
  ),
  [ 2618 ] => array(
    [ 9878767654 ] => array(
      [ 987987987 ] => 987987987, 
      [ 987987986 ] => 987987986,
      [ 987987985 ] => 987987985,
    ),
  ),
  [ 'tmp-9878767655' ] => array(
    [ 9878767655 ] => array(
      [ 987987975 ] => 987987975,
    ),
  ),
);

这是按商店 ID 列出的产品库存(按产品 ID 和型号 ID 列出)。我想合并模型 ID 值,其中产品 ID 与存储 ID 以“tmp-”开头的数组相同。如果产品 ID 不匹配,那么我希望该数组保持原样。我希望我有点道理。

请帮忙。

【问题讨论】:

    标签: php arrays merge compare


    【解决方案1】:

    这是一个解决您的示例提出的具体问题的sn-p:

    $temporaryStores = [];
    $prefix = 'tmp-';
    $prefixLength = strlen($prefix);
    
    // extract the temporary store structures
    foreach ($input as $storeId => $store) {
        if (is_string($storeId) && strpos($storeId, $prefix) === 0) {
            $productId = (int) substr($storeId, $prefixLength);
            $temporaryStores[$productId] = $store;
            unset($input[$storeId]);
        }
    }
    
    // merge matching temporary store structures into the actual ones
    $mergedProductIds = [];
    foreach ($temporaryStores as $temporaryProductId => $temporaryModels) {
        $temporaryModels = reset($temporaryModels); // Incompatible array structure
        foreach ($input as $storeId => $store) {
            foreach ($store as $productId => $models) {
                if ($productId === $temporaryProductId) {
                    $modelsIds = array_merge($temporaryModels, $models);
                    $modelsIds = array_unique($modelsIds);
                    $input[$storeId][$productId] = $modelsIds;
                    $mergedProductIds[] = $temporaryProductId;
                    unset($temporaryStores[$temporaryProductId]);
                }
            }
        }
    }
    
    // append leftover temporary store structures to the result
    foreach ($temporaryStores as $temporaryProductId => $temporaryModels) {
        if (!in_array($temporaryProductId, $mergedProductIds, true)) {
            $input[$prefix . $temporaryProductId] = $temporaryModels;
        }
    }
    
    var_dump($input);
    

    这个 sn-p 可能对你有用,也可能对你没用。无论哪种方式,我强烈建议您将此代码重构为使用更面向对象的设计。每个值/结构代表什么是显而易见的,并且可以单独进行验证。

    现在您不得不处理不兼容的数组结构,这些结构在视觉上看起来像是一团难以理解的混乱。

    【讨论】:

    • 非常感谢,我会试一试的。我同意你的评论。该数据来自多个数据库的查询,并且结构彼此略有不同。另一个因素是商店也想检测是否有重复的产品。感谢您的帮助。
    • 几乎就这样,它跳过了对具有重复产品 ID 的第二个商店 ID 的合并。我猜我将不得不重新考虑整个函数,会有数千种产品,并且为每个循环运行三个不同的产品会降低性能。
    • 我猜你可以在这个逻辑中添加(又一个)异常。但我希望您能找到一种方法将这段代码重组为更合乎逻辑(并且更易于使用)的 OOP 解决方案。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多