?
Solved

What is $unrolled_data?

Posted on 2016-07-25
9
Medium Priority
?
58 Views
Last Modified: 2016-07-26
Here's the function I'm attempting to deconstruct:

protected function pipeToArray($pipe_delim){

        $lines = explode("\n", str_replace("\r\n","\n",$pipe_delim));

        //Subsets of each statement
        $statement = array(
                'statement' => array(),
                'client' => array(),
                'guarantor' => array(),
                'patient' => array(),
                'unrolled_data' => array(),
                'rolled_data' => array(),
                'paymentplan_options' => array(),
                'paymentplan' =>array()
            );

        //Loop through the lines
        foreach ($lines as $raw_line) {

            //Split into cells
            $line = explode("|",$raw_line);

            switch ($line[0]) {
                  case 'N6' :
                    //Unrolled Account Information
                    $index = count($statement['unrolled_data']);
                    foreach($statement['unrolled_data'] as $i => $enc){
                        if($enc['encounterid'] === $line[1]) {
                            $index = $i;
                            continue;
                        }
                    }

                    $statement['unrolled_data'][$index]['txns'][] = array(

                        //'encounterid'   => $line[1],
                        //'provider_name' => $line[2],
                        'date'          => $line[3],
                        'type'          => $line[4],
                        'description'   => $line[5],
                        'amount'        => $line[6]
                        //'left_to_pay'   => $line[7]

                    );


                    break;
                case 'A6' :
                    //Rolled Account Information
                    $statement['rolled_data'][] = array(

                        'encounterid'   => $line[1],
                        'provider'      => $line[2],
                        'description'   => $line[3],
                        'date'          => $line[4],
                        'charged'       => $line[5],
                        'adjustments'   => $line[6],
                        'payments'      => $line[7],
                        'left_to_pay'   => $line[8]

                    );
                    break;
                }
        }
        return $statement;
    }

    /**
     * Return the pdf buffer string
     * @return String pdf buffer
     * @todo THESE ARE BEING HARDCODED TO TEMPLATEIDS... THIS SHOULD BE DONE ELSEWHERE, IN A BUILDER/FACTORY PERHAPS.
     */
    protected function toPDF(){
        if($this->statement_data['statement']['templateid'] == 4) {
            $image = new StandardStatementPDFGenerator($this);
        } else if ($this->statement_data['statement']['templateid'] == 9) {
            $image = new LetterStatementPDFGenerator($this);
        } else {
            //This is a "Fail Gracefully" measure. We haven't generated a statement
            //without a templateid since July 2013, so we shouldn't hit this moving
            //forward.
            $image = new StandardStatementPDFGenerator($this);
        }

        return $image->getStream();

    }

Open in new window


First part of my question: "unrolled_data" is not a column heading in a table. It is simply the name that's being given to a subset I'm about to create, correct?

Second part of my question: Beginning at line 26, I've got this:

//Unrolled Account Information
                    $index = count($statement['unrolled_data']);
                    foreach($statement['unrolled_data'] as $i => $enc){
                        if($enc['encounterid'] === $line[1]) {
                            $index = $i;
                            continue;
                        }
                    }

Open in new window


What's going on here?

$index=count($statement['unrolled_data']) // how can you count an array that has yet to be processed?

foreach($statement['unrolled_data'] as $I->$enc){ //where did $enc come from and what's happening here?

For the sake of clarity and brevity, I've attempted to streamline the function that I'm working on. What you see above is a portion of the actual code. I've got that below, just in case there's something missing from the puzzle that you need.

Bottom line: I'm trying to understand how "unrolled_data" is being defined and calculated.

What do you think?
0
Comment
Question by:brucegust
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 41728278
lets start with the simplest part:
foreach($statement['unrolled_data'] as $I->$enc){ 

this is a looping statement on an array, putting the key value unto $I and the value into $enc variable
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41728282
$index=count($statement['unrolled_data']) // how can you count an array that has yet to be processed?

remember that you are inside a loop, so it is being filled by prdvious iterations. on the first one, it will be 0 indeed.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41728289
so, how is it populated?
by lines where the first cell is 'N6':

switch ($line[0]) { case 'N6' :
...
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 

Author Comment

by:brucegust
ID: 41728334
Guy, my head hurts, trying to understand all of this, but let me try to explain this back to you:

I'm looping through a dataset. Every time the system registers the N6 cell, it's doing a couple of things:

In a regular array, you've got something like $line[1]. The "key" is "1" and the value is...? What?

I'm looking at $enc['encounter['id'] and it looks as though there's an if statement happening there, but what's happening?

Thanks for spelling this out for me...
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41728336
Man pages:
http://php.net/manual/en/control-structures.foreach.php
http://php.net/manual/en/function.count.php

This code is nearly incomprehensible, cannot be tested, and should be refactored.  Nobody writes this kind of spaghetti any more, at least not since the last century.  One of the problems I see here is that the behavior of the code is tightly coupled to the content of the data.  But because of the way this code is written you cannot provide mock-data.  So a logical step in trying to discern behavior would be to look at the data.  You can do that with var_dump().
http://php.net/manual/en/function.var-dump.php
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 total points
ID: 41728341
In a regular array, you've got something like $line[1]
In this case, the array is named $line, and a reference to $line includes the entire array.  The index 1 gets you to one position in the array, which is a mixed data structure  What it contains is not evident from the code, because it could contain almost anything.  There are no "real restricitons" on what you can put into a PHP array!  You can use var_dump() to see what's in there (check comment above for a link to the PHP man page about the var_dump() function).

Here is how arrays work in PHP.
http://php.net/manual/en/intro.array.php
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/language.operators.array.php

Multidimensional arrays are always somewhat confusing, and are rarely encountered outside of academic exercises.  Experienced programmers would use object-oriented designs to help avoid them!  TL;DR -- don't feel bad if you find this confusing.  Everyone finds this kind of code and data structure confusing!
0
 
LVL 31

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 200 total points
ID: 41728988
$statement as an array of arrays, it is first filled in case 'N6', though the first iteration must be empty, the place it's filled is the code part:

$statement['unrolled_data'][$index]['txns'][] = array(

                        //'encounterid'   => $line[1],
                        //'provider_name' => $line[2],
                        'date'          => $line[3],
                        'type'          => $line[4],
                        'description'   => $line[5],
                        'amount'        => $line[6]
                        //'left_to_pay'   => $line[7]

                    );

Open in new window


This is some of the split line parts, the fields, ("unrolled data", not a term I'd use) to create an array and add that at the end of the array $statement['unrolled_data'][$index]['txns'] via the empty [], this means append a new element, as in $line = array(); $line[]='line1'; $line[]='line2';

I agree with Ray, this is spaghetti code, today we rather do tiers, layers, like lasagne.

Bye, Olaf.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41729721
lol, I love Lasagne :-)
1
 

Author Comment

by:brucegust
ID: 41729728
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

568 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