2015-06-15 93 views
-1

我想將兩個數組與一個公共列組合。但是,並沒有得到我想要的。請檢查以下要求。PHP數組合並使用一列值

陣列1

Array 
(
[0] => stdClass Object 
    (
     [fieldLabel] => fname 
     [uuid] => 27478 
    ) 

[1] => stdClass Object 
    (
     [fieldLabel] => Lname 
     [uuid] => 6103 
    ) 

[2] => stdClass Object 
    (
     [fieldLabel] => Country 
     [uuid] => 7350 
    ) 

[3] => stdClass Object 
    (
     [fieldLabel] => check1 
     [uuid] => 23155 
    ) 

[4] => stdClass Object 
    (
     [fieldLabel] => radio1 
     [uuid] => 15664 
    ) 

) 

陣列2

Array 
(
[0] => stdClass Object 
    (
     [uuid] => 27478 
     [value] => sai1 
    ) 

[1] => stdClass Object 
    (
     [uuid] => 6103 
     [value] => sai2 
    ) 

[2] => stdClass Object 
    (
     [uuid] => 7350 
     [value] => USA 
    ) 

[3] => stdClass Object 
    (
     [uuid] => 23155 
     [value] => USA|India 
    ) 

) 

我需要像下面輸出。這兩個數組的'UUID'是很常見的。如果第二個數組中的值不存在,它應該是空的。

陣列 ( [0] => stdClass的對象 ( [FieldLabel聯合] => FNAME [UUID] => 27478 [值] => SAI1 )

[1] => stdClass Object 
    (
     [fieldLabel] => Lname 
     [uuid] => 6103 
     [value] =>sai2 
    ) 

[2] => stdClass Object 
    (
     [fieldLabel] => Country 
     [uuid] => 7350 
     [value] =>USA 
    ) 

[3] => stdClass Object 
    (
     [fieldLabel] => check1 
     [uuid] => 23155 
     [value] =>USA|India 
    ) 

[4] => stdClass Object 
    (
     [fieldLabel] => radio1 
     [uuid] => 15664 
    ) 

) 

請提供如何才能實現

+0

請分享你已經試過的東西。 –

回答

1

你可以遍歷這些數組,並進行如下更改:

foreach ($array1 as &$a1val) { 
    $value = 0; 
    foreach ($array2 as $a2val) { 
     if($a1val->uuid == $a2val->uuid) { 
      $value = $a2val->value; 
      break; 
     } 
    } 
    $a1val->value = $value; 
} 

P.S. :

  1. &用於通過引用更新$ array1。
+0

感謝您的回覆。它的工作正常。我添加了小代碼..即如果(isset($ a1val-> uuid))...我得到錯誤沒有驗證.. – Kishore

+0

很高興成爲任何幫助:) –

0
foreach($arr1 as $key1 => val1){ 
      foreach($arr2 as $key2 => $val2){ 
       if($val1['uuid'] == $val2['uuid']){ 
        $arr1[$key1]['value'] = $val2['value']; 
       } 
      } 
     } 
print_r($arr1); // this should give desire output