2016-11-24 2 views
0

Welche Funktion sollte ich verwenden, um array 0 mit array 1 basierend auf extref zu kombinieren. Die Kombination des Arrays ist wahr, wenn der Wert extref mit array 0 und array 1 übereinstimmt.Funktion php Array zu kombinieren

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 
           ) 

         )) 

das Ergebnis sollte wie wie folgt aussehen: -

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

Können Sie zeigen, was das Ergebnis aussehen soll? – Barmar

+0

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

+0

Edt die Frage zu zeigen, was Sie wollen, nicht in einen Kommentar. – Barmar

Antwort

0

Vergleichen Sie die beiden extref, und verwenden Sie array_merge, wenn sie die gleichen sind.

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 ist eine Möglichkeit, dies mit beliebigen Transaktionen zu erreichen:

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']); 
    } 
}