2016-11-24 66 views
0

根據extref,我應該使用什麼函數來組合array 0array 1。如果值extrefarray 0array 1相同,則該陣列的組合爲真。函數php來組合陣列

Array 
([Transaction] => Array 
     ([0] => Array //firts index array 
       ([extref] => WVB4108-10002761 
        [Denominations] => Array 
         ([Denomination] => Array 
           ([0] => Array 
             (
              [denomvalue] => 0.10 
              [denomqty] => 5 
              [denomcount] => 0.50 
             ) 

            [1] => Array 
             (
              [denomvalue] => 0.50 
              [denomqty] => 2 
              [denomcount] => 1.00 
             ) 

           )))) 

      [1] => Array //second index array 
       ([extref] => WVB4108-10002761 
        [Denominations] => Array 
         ([Denomination] => Array 
           (
            [denomvalue] => 1.00 
            [denomqty] => 13 
            [denomcount] => 13.00 
           ) 

         )) 

的結果應該如下: -

Array 
([Transaction] => Array 
     ([0] => Array 
       ([extref] => WVB4108-10002761 
        [Denominations] => Array 
         ([Denomination] => Array 
           ([0] => Array 
             (
              [denomvalue] => 0.10 
              [denomqty] => 5 
              [denomcount] => 0.50 
             ) 

            [1] => Array 
             (
              [denomvalue] => 0.50 
              [denomqty] => 2 
              [denomcount] => 1.00 
             ) 

            [2] => Array 
             (
              [denomvalue] => 1.00 
              [denomqty] => 13 
              [denomcount] => 13.00 
             ) 

           )))) 
+0

你能顯示結果應該是什麼樣子? – Barmar

+0

'if($ array ['Transaction'] [0] ['extref'] == $ array ['Transaction'] [1] ['extref']){$ result = true; } else {$ result = false; }' – Barmar

+0

Edt這個問題顯示你想要什麼,不要把它放在評論中。 – Barmar

回答

0

比較兩個extref,並使用array_merge如果它們是相同的。

if ($array['Transaction'][0]['extref'] == $array['Transaction'][1]['extref']) { 
    $array['Transaction'][0]['Denominations']['Denomination'] = 
     array_merge($array['Transaction'][0]['Denominations']['Denomination'], $array['Transaction'][1]['Denominations']['Denomination']); 
     unset($array['Transaction'][1]); 
} 
0
<?php 
    $array = .... 
    $documentation = []; 
    foreach($array['Transaction'] as $transaction) 
    { 
     $extref = $transaction['extref']; 
     if(!isset($documentation[$extref])) 
      $documentation[$extref] = []; 
     $documentation[$extref] = array_merge($documentation[$extref], $transaction['Documentations']['Documentation']) 
    } 

    $o = []; 
    foreach($documentation as $key => $value) 
    { 
     $o[]=array('extref' => $key, 'Documentations' => array('Documentation' => $value)); 
    } 

    return array('Transaction' => $o); 
0

Here的方式任意交易來實現這一點:

function cmp($a, $b) { 
    if ($a['extref'] == $b['extref']) 
     return 0; 
    return ($a['extref'] < $b['extref']) ? -1 : 1; 
} 

uasort($arr['Transaction'], 'cmp'); 

for($i = 0; $i < count($arr['Transaction'])-1 ; $i++){ 
    if ($arr['Transaction'][$i]['extref'] == $arr['Transaction'][$i+1]['extref']){ 
     $arr['Transaction'][$i] = array_merge_recursive($arr['Transaction'][$i],array_splice($arr['Transaction'],$i+1,1)[0]); 
     $arr['Transaction'][$i]['extref'] = reset($arr['Transaction'][$i]['extref']); 
    } 
}