Bruce Gust
asked on
Why does this work?
Here's the code:
Here's a screen shot of the results:
Perfect!
Basically, I need an array within an array when I've got a group of arrays that have the same date.
Awesome!
But I do not understand how by looking at the date of the next array, determining that the current date and the next date don't match, you can wind up with the "payments" array.
Here comes array #1: array("eventdate"=>"2016-0 8-26", "eventtype"=>"transaction" , "runningbal"=>"21.50"),
I'm looking at the date of the next array which is 2016-08-26. They're the same, so I'm going to add this to the $txnGroup array and finish.
Here comes array #2: array("eventdate"=>"2016-0 8-26", "eventtype"=>"transaction" , "runningbal"=>"32.50"),
I'm looking ahead and the dates are NOT the same, so I'm going to move ahead, the current $txnGroup is equal to "1," so...
Bottom line: There's a flow that results in a grouping of transactions that have the same date. It's like a digital version of "Mouse Trap," and while I'm watching it work, I get lost.
How is this working?
<?php
$data['timeline'] = array();
$txnGroup = array();
$michelle=array(
array("eventdate"=>"2016-08-26", "eventtype"=>"transaction", "runningbal"=>"21.50"),
array("eventdate"=>"2016-08-26", "eventtype"=>"transaction", "runningbal"=>"32.50"),
array("eventdate"=>"2016-08-27", "eventtype"=>"transaction", "runningbal"=>"32.50"),
array("eventdate"=>"2016-08-27", "eventtype"=>"statement", "runningbal"=>"50.50"),
);
for ($i=0; $i < count($michelle); $i++) {
$event = $michelle[$i];
//var_dump($event);
if($event['eventtype']=='transaction')
{
$thisDate = Date('mdy', strToTime($event['eventdate']));
$nextDate = (isset($michelle[$i+1])&&$michelle[$i+1]['eventtype']=='transaction')?Date('mdy', strToTime($michelle[$i+1]['eventdate'])):null;
array_push($txnGroup, $event);
//var_dump($event);
if ($thisDate <> $nextDate) {
//add to data
if (count($txnGroup)!=1) {
echo "yes";
array_push($data['timeline'], array(
'eventdate'=>Date('m/d/Y', strToTime($event['eventdate'])),
'eventtype'=>'payments',
'runningbal'=>$event['runningbal'],
'payments'=>$txnGroup
));
} else {
//this is where you left off. You've got to figure out how to
array_push($data['timeline'], $event);
}
$txnGroup = array();
}
}
else
{
array_push($data['timeline'], $event); //anything that's not a transaction event type gets put into the $data['timeline'] array as a regular $event
}
}
var_dump($data['timeline']);
foreach($data['timeline'] as $data)
{
echo $data['eventtype'].'<br>';
}
echo "<br>";
//var_dump($txnGroup);
?>
Here's a screen shot of the results:
Perfect!
Basically, I need an array within an array when I've got a group of arrays that have the same date.
Awesome!
But I do not understand how by looking at the date of the next array, determining that the current date and the next date don't match, you can wind up with the "payments" array.
Here comes array #1: array("eventdate"=>"2016-0
I'm looking at the date of the next array which is 2016-08-26. They're the same, so I'm going to add this to the $txnGroup array and finish.
Here comes array #2: array("eventdate"=>"2016-0
I'm looking ahead and the dates are NOT the same, so I'm going to move ahead, the current $txnGroup is equal to "1," so...
Bottom line: There's a flow that results in a grouping of transactions that have the same date. It's like a digital version of "Mouse Trap," and while I'm watching it work, I get lost.
How is this working?
Bruce: Please use var_export() to capture the output that is currently posted in image form. Please post that here in the code snippet, thanks.
From what I can see the code is gatthering transactions for a day and putting them into a payments array which is added to the timeline.
The code does a pre-add - in other words it adds the current item to the txnGroup array and then checks to see if the next transaction also belongs - if it does not - it means it is time to close this group off - so it creates the payment entry and then resets and the next transaction becomes part of a new group.
Not sure if that addresses the question?
The code does a pre-add - in other words it adds the current item to the txnGroup array and then checks to see if the next transaction also belongs - if it does not - it means it is time to close this group off - so it creates the payment entry and then resets and the next transaction becomes part of a new group.
Not sure if that addresses the question?
Not sure I understand the question either! What kind of output are you looking to create from the data in the $michelle array?
ASKER
The output that I'm getting is exactly what's supposed to happen. I'm not trying to correct anything. The end result that you see is exactly as it should be. I just don't understand WHY it works.
The first two arrays have the same date and they correctly find themselves in the "payments" array. How? I don't get how by looking ahead to see if the date is different and the current $txnGroup array is not "1," that the first two arrays are grouped together. They should be and they are. But I don't understand how those first two IF clauses can produce that anomaly.
The first two arrays have the same date and they correctly find themselves in the "payments" array. How? I don't get how by looking ahead to see if the date is different and the current $txnGroup array is not "1," that the first two arrays are grouped together. They should be and they are. But I don't understand how those first two IF clauses can produce that anomaly.
ASKER
Ray, here is the $michelle array when I do a var_dump:
array (size=4)
0 =>
array (size=3)
'eventdate' => string '2016-08-26' (length=10)
'eventtype' => string 'transaction' (length=11)
'runningbal' => string '21.50' (length=5)
1 =>
array (size=3)
'eventdate' => string '2016-08-26' (length=10)
'eventtype' => string 'transaction' (length=11)
'runningbal' => string '32.50' (length=5)
2 =>
array (size=3)
'eventdate' => string '2016-08-27' (length=10)
'eventtype' => string 'transaction' (length=11)
'runningbal' => string '32.50' (length=5)
3 =>
array (size=3)
'eventdate' => string '2016-08-27' (length=10)
'eventtype' => string 'statement' (length=9)
'runningbal' => string '50.50' (length=5)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
BAM!