2016-11-24 63 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']); 
    } 
}