?
Solved

Why does this work?

Posted on 2016-09-02
7
Medium Priority
?
72 Views
Last Modified: 2016-09-04
Here's the code:

<?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);

 

?>

Open in new window


Here's a screen shot of the results:

why
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-08-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-08-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?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41781781
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.
0
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 41781785
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?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41781792
Not sure I understand the question either!  What kind of output are you looking to create from the data in the $michelle array?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:brucegust
ID: 41782209
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.
0
 

Author Comment

by:brucegust
ID: 41782213
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)

Open in new window

0
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41782246
The result has the last transaction for a date as the parent.
If there is more than 1  transaction for a particular date then the payments member contains the history of all transactions on that date - if there is only 1 payment on a date there is no history - I guess because the root record is self explanatory.

Therefore the != 1 is there to check if a payments element should be created or not.

Another way of looking at
Find a record
Add it to txnGroup - we know we can do this because we check later to see if records are different and reset the array (line 37)
No we get to the date check - which means push into the timeline - but we have two cases
1. There is only one payment on the day
2. There is more than one
If there is one - we don't want to add a payments member - we only want to add the transaction
If there is more than one then we want to set the parent to the data of the most recent transaction and add the history under payments.
This is where the != 1 comes in. There will never be 0 line 19 makes sure of that.
If it is 1 it means we only had 1 transaction for the day - so don't make a payments member
If it is != 1 it actually means > 1 so that means create a payments member.
0
 

Author Closing Comment

by:brucegust
ID: 41783595
BAM!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question