1

array_merge_recursive で同じキーと値を持つ多次元配列をマージして上書きする方法は? 以下のような2つの配列があるとします:

$arr1 = array(       
   // OVERWRITE
   array('prop_id' => 1, 'prop_value' => 'batman'),
   array('prop_id' => 2, 'prop_value' => 'ironman'),
   // NOT OVERWRITE
   array('prop_id' => 5, 'prop_value' => 'wonderwoman'),
);

$arr2 = array(
   array('prop_id' => 1, 'prop_value' => 'robin'),
   array('prop_id' => 2, 'prop_value' => 'superman'),
   array('prop_id' => 4, 'prop_value' => 'catwoman'),
);

マージして新しい値で上書きしたい(ルールは上書きしない同じ値の比較キーです)、期待される結果は

 $result = array_merge_overwrite($arr1, $arr2, array('prop_id') /* Comparison Key */);
 $result = array(       
   array('prop_id' => 1 /* Comparison Key */, 'prop_value' => 'robin' /* Comparison value */),
   array('prop_id' => 2, 'prop_value' => 'superman'),
   array('prop_id' => 4, 'prop_value' => 'catwoman'),
   array('prop_id' => 5, 'prop_value' => 'wonderwoman'),
);

array_merge_recursive を使用すると、上書きされずに追加されます。以下のように array_replace_recursive を試します。

$result = array_replace_recursive(
    array(
       1 => array('prop_id' => 1, 'prop_value' => 'batman'),
       2 => array('prop_id' => 2, 'prop_value' => 'ironman'),
       5 => array('prop_id' => 5, 'prop_value' => 'wonderwoman'),
    ),
    array(
       1 => array('prop_id' => 1, 'prop_value' => 'robin'),
       2 => array('prop_id' => 2, 'prop_value' => 'superman'),
       4 => array('prop_id' => 4, 'prop_value' => 'catwoman'),
    ),
);

それは機能しますが、私のコードは厄介で汚いように見えます。私よりも良い解決策

4

2 に答える 2

1

あなたが説明したように機能する関数は次のとおりです。

function array_merge_overwrite($arr1, $arr2, $uniques=array('prop_id'), $delimiter='-')
{
    $result = array();
    $uk = array();
    foreach($arr1 as $a1)
    {   
        $uk = array();
        foreach($uniques as $u) $uk[] = $a1[$u];
        $result[implode($delimiter, $uk)] = $a1;
    }   

    foreach($arr2 as $a2)
    {   
        $uk = array();
        foreach($uniques as $u) $uk[] = $a2[$u];
        $result[implode($delimiter, $uk)] = $a2;
    }   
    return $result;
}

渡さ$arr1$arr2、質問で定義されている場合、上記の関数は配列を返します。

Array
(
    [1] => Array
        (
            [prop_id] => 1
            [prop_value] => robin
        )

    [2] => Array
        (
            [prop_id] => 2
            [prop_value] => superman
        )

    [5] => Array
        (
            [prop_id] => 5
            [prop_value] => wonderwoman
        )

    [4] => Array
        (
            [prop_id] => 4
            [prop_value] => catwoman
        )

)

もちろん、常にprop_id一意の要素としてのみ使用する場合、関数はかなり単純になる可能性があります。

function array_merge_overwrite($arr1, $arr2)
{
    $tmp = array();
    foreach($arr1 as $a1) $tmp[$a1['prop_id']] = $a1['prop_value'];
    foreach($arr2 as $a2) $tmp[$a2['prop_id']] = $a2['prop_value'];
    $result = array();
    foreach($tmp as $k=>$v) $result[] = array('prop_id'=>$k, 'prop_value'=>$v);
    return $result;
}

prop_idこの後の関数で返される配列の唯一の違いは、要素配列のキーがsに一致するのではなく、標準の数値になることです。

于 2012-07-30T17:46:48.590 に答える
0

As I can see is that you want to preserve the second array elements where in case when key of first array and second array are matching.

PHP manual says that: If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator:

<?php
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = $array1 + $array2;
var_dump($result);
?> 

The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

O/P

array(5) {
  [0]=>
  string(6) "zero_a"
  [2]=>
  string(5) "two_a"
  [3]=>
  string(7) "three_a"
  [1]=>
  string(5) "one_b"
  [4]=>
  string(6) "four_b"
}

For more detail look here

于 2012-07-30T03:33:47.103 に答える