2016-08-18 17 views
0

同じIDに基づいて2つの多次元配列をマージするときに、1つの問題に直面しています。同じIDに基づいて2つの多次元配列をマージする

以下の例では、Array1Array2の2つの配列を作成しました。両方の配列には、IDプロパティを持つオブジェクトが含まれています。 IDプロパティに基づいて、配列が合併し、結果の配列を取得する必要があります。ARRAY2

Array 
(
[0] => stdClass Object 
    (
     [ID] =>2 
     [name] => test1 

    ) 

[1] => stdClass Object 
    (
     [ID] => 3 
     [name] => test2 
    ) 
[2] => stdClass Object 
    (
     [ID] =>4 
     [name] => test3 
    ) 

[3] => stdClass Object 
    (
     [ID] => 5 
     [name] => test4 
    ) 
) 

Result_array

Array 
(
[0] => stdClass Object 
    (
     [ID] =>2 
     [name] => test1 
     [claimtotal] => 
     [total] => 
    ) 

[1] => stdClass Object 
    (
     [ID] => 3 
     [name] => test2 
     [claimtotal] => 
     [total] => 4 
    ) 
[2] => stdClass Object 
    (
     [ID] =>4 
     [name] => test3 
     [claimtotal] => 20 
     [total] => 1 
    ) 

[3] => stdClass Object 
    (
     [ID] => 5 
     [name] => test4 
     [claimtotal] => 
     [total] => 
    ) 
) 

ARRAY1

Array 
(
[0] => stdClass Object 
    (
     [claimtotal] => 
     [total] => 4 
     [ID] => 3 

    ) 

[1] => stdClass Object 
    (
     [claimtotal] => 20 
     [total] => 1 
     [ID] => 4 
    ) 
) 

を私はこれを達成するにはどうすればよいです?

+1

希望の結果を得るために試したことをお見せください – Epodax

答えて

1

これらは、メソッドずに単純なオブジェクトである場合に行く:

foreach($firstArray as $key => $firstObject){ 
foreach($secondArray as $secondObject){ 
    if($firstObject['id'] === $secondObject['id']){ 
     $firstArray[$key] = (object) array_merge((array) $firstObject, (array) $secondObject); 
    }    
    } 
} 

が乱雑に見えますが、オブジェクトのプロパティを経由して別のループを導入することなく仕事をしていません。

関連する問題