2017-02-14 106 views
0

我有一個2d數組。取一個數組,並添加一個字段與來自數組的數據

$original_array = array(); 
// amount/funding_by/withdrawing_by/bank_name_1/bank_name_2 
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "ANZ"); 
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "BOA"); 
$original_array[] = array("100","Cash","Bank Deposit", "Chase", "BOA"); 
$original_array[] = array("100","Bank Deposit","Cash", "Chase", "BOA"); 
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "JP Morgan"); 

我想添加通過取funding_by和withdrawing_by字段,並用「到」分離形成的附加字段(funding_by_to_withdrawing_by)。 也就是說。銀行存款到銀行存款

最後,我想壓縮數組,檢查數額bank1_name和bank_2_name是否都與另一行相匹配,如果是,則將原始行的funding_by_to_withdrawing_by字段與副本的funding_by_to_withdrawing_by字段相加行。

重複行隨後被刪除,所以處理的陣列看起來像這樣:

$processed_array = array(); 
// amount/funding_by/withdrawing_by/bank_name_1/bank_name_2/funding_by_to_withdrawing_by 
$processed_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "ANZ", "Bank Deposit to Bank Deposit"); 
$processed_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "BOA", "Bank Deposit to Bank Deposit, Cash to Bank Deposit, Bank Deposit to Cash"); 
$processed_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "JP Morgan"); 

我已經試過這一點,但是說實話,我只是得到了更多的疑惑,這是行不通的:

$original_array = array(); 
// amount/funding_by/withdrawing_by/bank_name_1/bank_name_2 
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "ANZ"); 
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "BOA"); 
$original_array[] = array("100","Cash","Bank Deposit", "Chase", "BOA"); 
$original_array[] = array("100","Bank Deposit","Cash", "Chase", "BOA"); 
$original_array[] = array("100","Bank Deposit","Bank Deposit", "Chase", "JP Morgan"); 

$processed_array = array(); 
foreach($original_array as $submitted_data) 
{ 
    foreach($processed_array as $verified_data) 
    {  
     if ($submitted_data[0]==$verified_data[0]) 
     { 
      $duplicate_found = true; 
      $listoffundingtowithdrawalmethods = "$fundingname to $withdrawalname<br>" . $submitted_data[1] . " to " . $submitted_data[2]; 
      array_push($verified_data, "$listoffundingtowithdrawalmethods"); 
      $processed_array[] = $verified_data; 
     } 
     else 
     { 
      $duplicate_found = ""; 
     } 
    } 
    if ($duplicate_found != true) 
    { 
     $listoffundingtowithdrawalmethods = "$fundingname to $withdrawalname<br>"; 
     array_push($submitted_data, "$listoffundingtowithdrawalmethods"); 
     $processed_array[] = $submitted_data; 
    } 
} 

然而它仍然不是很正確。我希望你們能夠有所作爲。

謝謝你的時間!

馬特

+0

由於您的原始數組沒有任何種類的唯一標識符,因此您似乎可能擁有似乎是重複的條目,但實際上是不同的事務。那可能嗎? –

+0

感謝您的關注!它們實際上不是交易,它們是送錢的方式(銀行存款,現金等到銀行存款,現金),以及如果您發送了錢,您會得到什麼。所以,如果一種方式發送金錢獲得與另一種方式相同的金額,我想將其壓縮成一行。 –

+0

啊,好的。我懂了。 –

回答

0

我會用不同的方法。首先,原始數組轉換成一個分層陣列:

foreach ($original_array as $row) { 
    $intermediate[$row[0]][$row[3]][$row[4]][] = "$row[1] to $row[2]"; 
} 

這將使一個這樣的數組:

[ 
    100 => [ 
     'Chase' => [ 
      'ANZ' => ['Bank Deposit to Bank Deposit'], 
      'BOA' => [ 
        'Bank Deposit to Bank Deposit', 
        'Cash to Bank Deposit', 
        'Bank Deposit to Cash'], 
      'JP Morgan' => ['Bank Deposit to Bank Deposit'] 
     ] 
    ] 
]; 

然後重複這三個層次的中間陣列來產生最終結果。

foreach ($intermediate as $amount => $bank1_names) { 
    foreach ($bank1_names as $bank1_name => $bank2_names) { 
     foreach ($bank2_names as $bank2_name => $transfers) { 
      $result[] = [$amount, $bank1_name, $bank2_name, implode(", ", $transfers)]; 
     } 
    } 
} 

這將只需要通過原始數據集相當於兩次。其結果將是這樣的:

[ 
    [100, 'Chase', 'ANZ', 'Bank Deposit to Bank Deposit'], 
    [100, 'Chase', 'BOA', 'Bank Deposit to Bank Deposit, Cash to Bank Deposit, Bank Deposit to Cash'], 
    [100, 'Chase', 'JP Morgan', 'Bank Deposit to Bank Deposit'] 
] 

它不包括"funding_by""withdrawing_by"列,但因爲你是所有這些組合成"funding_by_to_withdrawing_by"它似乎並不像這些都是有意義的任何更多的反正。

+0

謝謝!更簡單,它完美的作品!祝你有美好的一天!馬特 –

+0

不客氣。你也是! –

相關問題