【问题标题】:Merge multidemensional arrays with different length in PHP在PHP中合并不同长度的多维数组
【发布时间】:2016-02-14 20:58:45
【问题描述】:

我需要合并两个具有以下格式的数组:

array(9) 
{ 
[0]=> array(1) { ["BLA"]=> string(7) "bis 050" } 
[1]=> array(1) { ["BLA"]=> string(7) "bis 060" } 
[2]=> array(1) { ["BLA"]=> string(7) "bis 070" } 
[3]=> array(1) { ["BLA"]=> string(7) "bis 080" } 
[4]=> array(1) { ["BLA"]=> string(7) "bis 090" } 
[5]=> array(1) { ["BLA"]=> string(7) "bis 100" } 
[6]=> array(1) { ["BLA"]=> string(7) "bis 110" } 
[7]=> array(1) { ["BLA"]=> string(7) "bis 120" } 
[8]=> array(1) { ["BLA"]=> string(6) "gr 120" } 
} 

array(5) 
{ 
[0]=> array(2) {
   ["BLA"]=> string(7) "bis 050" 
   ["Amount"]=> string(3) "832" } 
[1]=> array(2) { 
   ["BLA"]=> string(7) "bis 060" 
   ["Amount"]=> string(3) "448" } 
[2]=> array(2) { 
   ["BLA"]=> string(7) "bis 090" 
   ["Amount"]=> string(4) "1216" } 
[3]=> array(2) { 
   ["BLA"]=> string(7) "bis 100" 
   ["Amount"]=> string(4) "1024" } 
[4]=> array(2) { 
   ["BLA"]=> string(7) "bis 110" 
   ["Amount"]=> string(3) "896" } 
}

我尝试了array_merge()array_merge_recursive(),但它不起作用。 我的目标是将第二个键及其值从array2(Amount)写入数组1,其中第一个键(BLA)的值相同。另外我想写"Amount":"0",如果array2中没有对应的值。有没有办法用php做到这一点?

结果应如下所示:

    Result:
{ 
[0]=> array(2) { 
   ["BLA"]=> string(7) "bis 050"  
   ["Amount"]=> string(3) "832" } 
[1]=> array(2) { 
   ["BLA"]=> string(7) "bis 060"  
   ["Amount"]=> string(3) "448" } 
[2]=> array(2) { 
   ["BLA"]=> string(7) "bis 070" 
   ["Amount"]=> string(1) "0" }  
[3]=> array(2) { 
   ["BLA"]=> string(7) "bis 080" 
   ["Amount"]=> string(1) "0" }  
[4]=> array(2) { 
   ["BLA"]=> string(7) "bis 090" 
   ["Amount"]=> string(4) "1216" }
[5]=> array(2) { 
   ["BLA"]=> string(7) "bis 100" 
   ["Amount"]=> string(4) "1024" }  
[6]=> array(2) { 
   ["BLA"]=> string(7) "bis 110" 
   ["Amount"]=> string(3) "896" } 
[7]=> array(2) { 
   ["BLA"]=> string(7) "bis 120" 
   ["Amount"]=> string(1) "0" }  
[8]=> array(2) { 
   ["BLA"]=> string(6) "gr 120" 
   ["Amount"]=> string(1) "0" } 
}   

【问题讨论】:

  • 数组键必须是唯一的
  • stackoverflow.com/a/4769240/1604068 这可能会对你有所帮助
  • 不仅是合并,还改变了默认结构。另一个数组中的键没有退出时发生了什么?
  • @Dagon 他们是独一无二的......
  • 我同意 Dagon 的观点,但它的布局方式似乎键是唯一的,但数组的主索引是隐藏的数字索引。 IE: '0 => array("BLA" => "bis 050", "Amoun......

标签: php arrays


【解决方案1】:

我整理了一个快速演示,似乎可以解决您的问题。如果您需要任何其他帮助,请告诉我。

<?php

$arrOne = array(
    array("BLA" => "bis 050"),
    array("BLA" => "bis 060"),
    array("BLA" => "bis 070"),
    array("BLA" => "bis 080"),
    array("BLA" => "bis 090"),
    array("BLA" => "bis 100"),
    array("BLA" => "bis 110"),
    array("BLA" => "bis 120"),
    array("BLA" => "gr 120")
);

$arrTwo = array(
    array("BLA" => "bis 050","Amount" => "832"),
    array("BLA" => "bis 060","Amount" => "448"),
    array("BLA" => "bis 090","Amount" => "1216"),
    array("BLA" => "bis 100","Amount" => "1024"),
    array("BLA" => "bis 110","Amount" => "896")
);


$arrOutput = array();

foreach($arrOne as $arrOneValue) {
    $searchKey = $arrOneValue["BLA"];

    foreach($arrTwo as $arrTwoValue) {
        if($arrTwoValue["BLA"] == $searchKey) {
            $arrOutput[] = array("BLA" => $searchKey, "Amount" => $arrTwoValue["Amount"]);
            continue 2; // Continue the outer loop
        }
    }

    // We didn't find the key
    $arrOutput[] = array("BLA" => $searchKey, "Amount" => "0");
}

var_dump($arrOutput);
?>

这会产生如下内容:

array(9) { 
    [0]=> array(2) { ["BLA"]=> string(7) "bis 050" ["Amount"]=> string(3) "832" } 
    [1]=> array(2) { ["BLA"]=> string(7) "bis 060" ["Amount"]=> string(3) "448" } 
    [2]=> array(2) { ["BLA"]=> string(7) "bis 070" ["Amount"]=> string(1) "0" } 
    [3]=> array(2) { ["BLA"]=> string(7) "bis 080" ["Amount"]=> string(1) "0" } 
    [4]=> array(2) { ["BLA"]=> string(7) "bis 090" ["Amount"]=> string(4) "1216" } 
    [5]=> array(2) { ["BLA"]=> string(7) "bis 100" ["Amount"]=> string(4) "1024" } 
    [6]=> array(2) { ["BLA"]=> string(7) "bis 110" ["Amount"]=> string(3) "896" } 
    [7]=> array(2) { ["BLA"]=> string(7) "bis 120" ["Amount"]=> string(1) "0" } 
    [8]=> array(2) { ["BLA"]=> string(6) "gr 120" ["Amount"]=> string(1) "0" } 
} 

【讨论】:

  • 谢谢。一般来说,我建议您考虑重新排列您拥有的数组,以便您可以利用 PHP 支持的一些不错的数组合并/通用功能。简单的选择是删除“BLA”键并将“bis ###”部分作为键本身。不过,这完全取决于您在这里处理的内容,所以我将由您决定。
【解决方案2】:

试试这个:

$array1 = '[
{"BLA":"bis 050"},
{"BLA":"bis 060"},
{"BLA":"bis 070"},
{"BLA":"bis 080"},
{"BLA":"bis 090"},
{"BLA":"bis 100"},
{"BLA":"bis 110"},
{"BLA":"bis 120"},
{"BLA":"gr 120"}
]';

$array2 = '[
{"BLA":"bis 050","Amount":"832"},
{"BLA":"bis 060","Amount":"448"},
{"BLA":"bis 090","Amount":"1216"},
{"BLA":"bis 100","Amount":"1024"},
{"BLA":"bis 110","Amount":"896"}
]';

$a1 = json_decode($array1, TRUE);
$a2 = json_decode($array2, TRUE);
$result = [];

foreach ($a1 as $source) {
  $found = FALSE;
  foreach ($a2 as $key => $content) {
    if ($content['BLA'] == $source['BLA']) {
      $element = $content;
      $a2[$key]['BLA'] = NULL; // ensure not to reuse furtherly
      $found = TRUE;
      break;
    }
  }
  if (!$found) {
    $element = ['BLA' => $source['BLA'], 'Amount' => 0];
  }
  $result[] = $element;
}
// add unused $a2 content, if any
if ($a2) {
  $result += $a2;
}

echo '<pre>' . print_r(str_replace(",{", ",\n{", json_encode($result)), TRUE) . '</pre>';

【讨论】:

    【解决方案3】:

    你可以这样试试:

    $result = array_filter($array1, function (&$item) {
        $item['Amount'] = '0';
        return $item;
    });
    
    $index = array_column( $result,'BLA' );
    foreach( $array2 as $row )
    {
        $found = array_search( $row['BLA'], $index );
        if( $found === False )
        {
            $result[] = $row;
            $index[]  = $row['BLA'];
        }
        else
        {
            $result[$found]['Amount'] = $row['Amount'];
        }
    }
    
    print_r( $result );
    

    eval.in demo

    首先,我过滤数组1以添加到每个元素空Amount;然后我用BLA 值创建一个索引,以避免嵌套foreach 循环。 通过第二个数组的foreach 循环,我发现该键是否存在于修改后的数组中:如果存在,我更改“金额”键,否则我将一个新项目附加到数组中。

    通过这种方法,你只执行两个foreach循环(考虑array_filter作为foreach,三个也考虑array_column); whit 嵌套foreach,相反,您必须执行可变数量的循环,如果您有大型数组,则会影响性能。

    请注意:

    array_column 在 php >= 5.5 中可用。

    如果你使用以前的版本,你必须用这个替换相应的行:

    $index = array();
    foreach( $result as $row ) $index[] = $row['BLA'];
    

    【讨论】:

      猜你喜欢
      • 2020-01-17
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多