Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 如何基于深度值合并数组?_Php_Arrays_Multidimensional Array - Fatal编程技术网

Php 如何基于深度值合并数组?

Php 如何基于深度值合并数组?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我想在pro_code值上合并上面的两个数组。这是预期的结果: Array ( [0] => Array ( [OrderProduct] => Array ( [pro_code] => B1 [totalDistSalesQTY] => 360 ) ) [1] => Array ( [OrderProduct] => Array (

我想在
pro_code
值上合并上面的两个数组。这是预期的结果:

Array (
    [0] => Array (
        [OrderProduct] => Array (
            [pro_code] => B1
            [totalDistSalesQTY] => 360
        )
    )
    [1] => Array (
        [OrderProduct] => Array (
            [pro_code] => B2
            [totalDistSalesQTY] => 600
        )
    )
    [2] => Array (
        [OrderProduct] => Array (
            [pro_code] => B3
            [totalDistSalesQTY] => 600
        )
    )
)   

我想合并多个数组。请帮我把它合并好。我应用了许多技术来合并它。我仍然找不到我的路。请帮我解决这个问题。

因此,基本上,您要做的是在序列上实现一个。这是一个基本的编程论文,所以它不应该太难

首先,您必须按照一个唯一的、严格可排序的值(这里是
pro_代码
)按升序(示例中的数组)对序列进行排序

其次,您需要同时遍历这两个序列。 如果当前项的唯一键(
pro_code
)匹配,则将它们合并并推送到结果数组,如果不匹配,则将具有较小键的项推送到结果数组。推送后,将推送序列的索引增加1

到达其中一个序列的末尾后,将其余的推送到结果数组

// combine the two arrays together and iterate all their rows
foreach (array_merge($array1, $array2) as $row) {

    // get the code
    $code = $row['OrderProduct']['pro_code'];

    // get the previous value for that code, or an empty array if there isnt' one
    $prev = $result[$code]['OrderProduct'] ?? [];

    // merge the previous value with the new value
    $result[$code]['OrderProduct'] = array_merge($prev, $row['OrderProduct']);
}
这就是它在php中的实现方式:

Array (
    [0] => Array (
        [OrderProduct] => Array (
            [pro_code] => B1
            [totalDistSalesQTY] => 360
            [totalQTY] => 4
        )
    )
    [1] => Array (
        [OrderProduct] => Array (
            [pro_code] => B2
            [totalDistSalesQTY] => 600
            [totalQTY] => 4
        )
    )
    [2] => Array (
        [OrderProduct] => Array (
            [pro_code] => B3
            [totalDistSalesQTY] => 600
        )
    )
    [3] => Array (
        [OrderProduct] => Array (
            [pro_code] => B4
            [totalQTY] => 4
        )
    )
)

这是我能做的最精简的方法。这实际上是3个简单的步骤和一个自定义函数

代码:

自定义函数说明:
getDeepColumn()
使用指定数组和指定列名从所有
OrderProduct
子数组中提取所需值。列值临时存储在名为
$result
的数组中
$result
的第一级键是
pro\u code
值,用于将来的合并目的
$result
的子数组由指定列的名称(作为键)和指定列的值(作为值)组成

首先,使用将所需的深列值混合在一起

接下来,
$merge
(使用唯一的
pro_code
键)被编辑

最后,使用重新索引所有
OrderProduct
子数组,并将
pro_code
值返回到
OrderProduct
子数组中的正确位置

输出:

array (
  0 => 
  array (
    'OrderProduct' => 
    array (
      'pro_code' => 'B1',
      'totalQTY' => '4',
      'totalDistSalesQTY' => '360',
    ),
  ),
  1 => 
  array (
    'OrderProduct' => 
    array (
      'pro_code' => 'B2',
      'totalQTY' => '4',
      'totalDistSalesQTY' => '600',
    ),
  ),
  2 => 
  array (
    'OrderProduct' => 
    array (
      'pro_code' => 'B3',
      'totalDistSalesQTY' => '600',
    ),
  ),
  3 => 
  array (
    'OrderProduct' => 
    array (
      'pro_code' => 'B4',
      'totalQTY' => '4',
    ),
  ),
)

如果将产品代码用作结果数组中的键,那么这非常简单

// combine the two arrays together and iterate all their rows
foreach (array_merge($array1, $array2) as $row) {

    // get the code
    $code = $row['OrderProduct']['pro_code'];

    // get the previous value for that code, or an empty array if there isnt' one
    $prev = $result[$code]['OrderProduct'] ?? [];

    // merge the previous value with the new value
    $result[$code]['OrderProduct'] = array_merge($prev, $row['OrderProduct']);
}

您将在结果数组中得到产品代码键,这可能很好。如果不合适,可以使用
$result=array\u values($result)

删除它们。您尝试了什么?最后,您真正想要什么?是否尝试使用array\u diff()=>?
array (
  0 => 
  array (
    'OrderProduct' => 
    array (
      'pro_code' => 'B1',
      'totalQTY' => '4',
      'totalDistSalesQTY' => '360',
    ),
  ),
  1 => 
  array (
    'OrderProduct' => 
    array (
      'pro_code' => 'B2',
      'totalQTY' => '4',
      'totalDistSalesQTY' => '600',
    ),
  ),
  2 => 
  array (
    'OrderProduct' => 
    array (
      'pro_code' => 'B3',
      'totalDistSalesQTY' => '600',
    ),
  ),
  3 => 
  array (
    'OrderProduct' => 
    array (
      'pro_code' => 'B4',
      'totalQTY' => '4',
    ),
  ),
)
// combine the two arrays together and iterate all their rows
foreach (array_merge($array1, $array2) as $row) {

    // get the code
    $code = $row['OrderProduct']['pro_code'];

    // get the previous value for that code, or an empty array if there isnt' one
    $prev = $result[$code]['OrderProduct'] ?? [];

    // merge the previous value with the new value
    $result[$code]['OrderProduct'] = array_merge($prev, $row['OrderProduct']);
}