Matthew Goodwin
Matthew Goodwin

Reputation: 1

Taking an array, and adding a field with data from the array

I have a 2d array.

$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"); 

I'd like to add an additional field (funding_by_to_withdrawing_by) formed by taking the funding_by and withdrawing_by fields, and separating the with "to ". Ie. Bank Deposit to Bank Deposit

Finally I'd like to compress the array, checking if the amount, bank1_name, and bank_2_name all match another row, and if so then appending the original row's funding_by_to_withdrawing_by field with the funding_by_to_withdrawing_by field of the duplicate row.

The duplicate row can then be deleted, so the processed array looks like this:

$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");

I've tried this, however to be honest I just got more confused, and it doesn't work:

$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; 
    }
 } 

However it still isn't quite right.

Upvotes: 0

Views: 54

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

I would use a different approach. First convert the original array into a hierarchical array:

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

This will make an array like this:

[
    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']
        ]
    ]
];

Then iterate the three levels of the intermediate array to produce the final result.

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

This will only take the equivalent of two passes through your original data set. The result will be like this:

[
    [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']
]

It does not include the "funding_by" and "withdrawing_by" columns, but since you are combining all of those into "funding_by_to_withdrawing_by" it doesn't seem like those are meaningful any more anyway.

Upvotes: 1

Related Questions