0

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.

4
  • Since your original array doesn't have any kind of unique identifier, it seems like you might have entries that appear to be duplicates, but are actually different transactions. Is that possible? Commented Feb 14, 2017 at 22:45
  • Thanks for taking a look!. They're not actually transactions, they're ways of sending money (bank deposit, cash etc to bank deposit, cash), and what you'd get if you sent the money. So where one way to send the money gets you the same amount as another way, I'd like to compress it into one row. Commented Feb 14, 2017 at 22:54
  • From what I see, you're $processed_array is empty to begin with. I'm guessing you forgot to initialize it with the value of your $original_array. And in your foreach loop I see you're comparing $submitted_data[0] with $verified_data[0], but that would just compare the amounts. If you want to compare the bank names, you should do a similar comparison for the 2 arrays for index 3 and 4. And I see you're using these variables - $fundingname and $withdrawal name - but they are never initialized. Commented Feb 14, 2017 at 23:01
  • Just a few thoughts: $withdrawalname and $fundingname are not initialized; probably should be submitted_data[1/2]. $duplicate_found is not initialized on the first run (because $processed data is empty); I think you need to define it before the inner loop. It also doesn't seem right that you set it in the else clause. That way you could toggle it on and off for the same $submitted_data array. I hope this helps. ETA: slow pony :) Commented Feb 14, 2017 at 23:06

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dude! Much simpler and it works perfectly! Have a great day! Matt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.