[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Different output than wanted.

Posted on 2016-09-03
3
Medium Priority
?
80 Views
Last Modified: 2016-09-07
$find = '.5010.';

$dirname = '79';
if(!is_dir($dirname))
    mkdir($dirname, 0777);

$directory_with_files = './';
$dh  = opendir($directory_with_files);
$files = array();
while (false !== ($filename = readdir($dh)))
{
    if(in_array($filename, array('.', '..')) || is_dir($filename))
        continue;

    $files[] = $filename;
}


foreach($files as $file)
{
    //find only 5010 files
    if(stripos($file, $find) !== false)
    {
        // open the 5010 file
        $handle = fopen($file, "r");
        $file_content = file_get_contents($file);
     
        
        if ($handle) {
            $header = '';
            $name = '';
            $footer = '';
            $payor_blocks = array();
            
            
            // determine if file has more than one payor
            $payor_count = substr_count($file_content, 'N1*');
            //if the file has more than one payor
            if($payor_count > 1) {
                //read the file line by line
                $header_end = false;
                $block_start = false;
                $count = 1;

                
                while (($line = fgets($handle)) !== false) {
                    //look for the first payor block
                    if(strpos($line, 'N1*') !== false || $block_start) {
                        $header_end = true; $block_start = true;
                        //see if the block finished
                        if(strpos($line, 'CAS~') !== false) {
                            $block_start = false;
                            $payor_blocks[$count] .= $line;
                            $count++;
                        }
                        $payor_blocks[$count] .= $line;
                    } else {
                        //append to the header
                        if($header_end) {
                            $footer .= $line."\n";
                        } else {
                            $header .= $line."\n";
                        }
                    }
                }

                //get payor blocks and create a file foreach payor
                $new_files = array();
                foreach($payor_blocks as $block) {
                    $filename = $file . "_" . $count;
                    $count++;
                    $new_files[] = array(
                        'name' => $filename,
                        'content' => $header."\n".$block."\n".$footer	
                    	
                    );
                    
//footer prints out after the first CAS and all the rest
                }
                echo "<pre>" . print_r($new_files, true) . "</pre>";
                //loop through new files and create them
                foreach($new_files as $new_file) {
                    //create file
                	$myfile = fopen($file, "w");
      	
                	fwrite($myfile, $new_file['content']);	
              		//close the file
                	fclose($myfile);
                }
                
            } else {

            	// DO what u were doing in script #1
           while (($line = fgets($handle)) !== false)
            {
            	$refid = 'REF';
            	
            	if(stripos($line, $refid) !== false)
            	{
            		$refnumber = str_replace(array($refid, '~'), array('', ''), $line);
            		$refnumber = trim($refnumber);
            	
            		if($refnumber != '')
            		{
            			$refnumber = '_'.$refnumber.'_';
            			$filerenamed = str_replace($find, $refnumber, $file);
            			copy($file, $dirname.'/'.$filerenamed);
            		}
            		echo $refnumber . "\n";
            	}
            	}
            	 
            }
        }
        // DONE - close the file
        fclose($handle);
    }
}

Open in new window


The code above, I want it to read a file and split it into to, it splits them but doesn't create a file for each and i keep getting this warning: Notice: Undefined offset: 1 in /script2.php on line 62 Notice: Undefined offset: 2 in /script2.php on line 62
Im not sure why, also the N1* part shows the whole thing in one file and none of it in the second (i see this through my browser)
0
Comment
Question by:Jasmine Ikhreishi
[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
  • 2
3 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41782910
Undefined offset  is like "undefined variable" in that it means the array does not contain any information at the key named in the message.

This could not have occurred on line 62 in the code snippet posted above because that line does not contain an array reference.  When you post code here at E-E, please post the complete code snippet, so we can see how the line numbers in the code and the line numbers in the error messages are related!
0
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41782912
Okay, this is the line that is line 62
$payor_blocks[$count] .= $line;

Open in new window

Which is line 56 in my snippet code
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 41783243
$payor_blocks[$count] .= $line;

Open in new window

What does PHP do with this?  Here's an explanation.

1. PHP will identify the contents of the $line variable
2. PHP will identify the contents of the $count variable
3. PHP will locate the $payor_blocks variable and ensure that it is an array
4. PHP will try to use  the $count variable to locate an element in the array; this will fail because there is nothing in $payor_blocks that can be indexed by $count
5. PHP will take no further action, except to raise a Notice message
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
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.

649 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