Jump to content

Recommended Posts

Hi There.

 

To me, this is a tough one, hopefully someone has done this.  I am parsing through text files which have a main delimiter then in certian columns have other types of delimiters.  Its a long story :P

 

foreach (explode("\r\n",$a) as $b) {
$j = 1;
//str replace lines
if(!empty($rule)) {
$b = str_replace($rule,$aid,$b);
}

$b = mysql_real_escape_string(trim($b));

//end replace aid in line
$delimiter = "|";
//enter each delimiter seperation as array value
$vals = array();
foreach (explode($delimiter,$b) as $key => $value) {
$vals[$j] = $value;
    $j++;
}

//find specific columns and replace the obsure delimiters with the main one
$j = 1;
foreach($vals as $redelimit) {
if($j = $scat) {
$vals[$j] = str_replace(" > ", $delimiter, $redelimit);	
}
    
if($j = $sscat) {
$vals[$j] = str_replace("->", $delimiter, $redelimit);	
}
$j++;
}
unset($redelimit);
//SEE EXAMPLE BELOW $vals array should look like that.
//HERE IS WHERE I AM HAVING PROBLEMS.  NEED TO REPACK THE ARRAY
$j = 1;
$h = 1;
$newvals = array();
foreach($vals as $key => $redelimit)) {
if(substr_count($redelimit,$delimiter) > 0 ) { 
$vals[$
}

$h++;	
}

//Compare applicable column numbers and only pack parsevals with matching column values.
$x = 0;
$parsevals = array();
foreach($number as $restuff) {

$parsvals[$x] = $vals[$restuff];
$x++;
}

 

RAW Looks like this:  VALUE 1|VALUE 2|VALUE 3|VALUE 4 > VALUE 5 > VALUE 6|VALUE 7|VALUE 8->VALUE 9|VALUE 10

 

What happens after I seperate by the first delimiter is this:

 

$vals array

[1] = VALUE 1

[2] = VALUE 2

[3] = VALUE 3

[4] = VALUE 4 > VALUE 5 > VALUE 6

[5] = VALUE 7

[6] = VALUE 8->VALUE 9

[7] = VALUE 10

 

I then need to turn this into (in array numerical order like that) as this must match a master template of column positioning.

[1] = VALUE 1

[2] = VALUE 2

[3] = VALUE 3

[4] = VALUE 4

[5] = VALUE 5

[6] = VALUE 6

[7] = VALUE 7

[8] = VALUE 8

[9] = VALUE 9

[10] = VALUE 10

 

Hope someone can help! :)  :shrug:

 

 

Link to comment
https://forums.phpfreaks.com/topic/186965-rework-an-array/
Share on other sites

Well right now the current code does do the STR_REPLACE for the other delimiters.  Because other values may include the same characters, the STR REPLACE can only happen on pre-specified columns, that is why the code has to seperate by the main delimiter first.

(say main delimiter is $delimiter = "|";)

So here is the steps in this code:

 

$b = 'VALUE 1|VALUE 2|VALUE 3|VALUE 4 > VALUE 5 > VALUE 6|VALUE 7|VALUE 8->VALUE 9|VALUE 10';
$delimiter = "|";
//enter each delimiter seperation as array value
$vals = array();
foreach (explode($delimiter,$b) as $key => $value) {
   $vals[$j] = $value;
    $j++;
}

This is what $vals array looks like:

[1] = VALUE 1

[2] = VALUE 2

[3] = VALUE 3

[4] = VALUE 4 > VALUE 5 > VALUE 6

[5] = VALUE 7

[6] = VALUE 8->VALUE 9

[7] = VALUE 10

 

The next code takes the rest of the delimiters (based on $scat and $sscat column numbers);

 

$j = 1;
foreach($vals as $redelimit) {
   if($j = $scat) {
   $vals[$j] = str_replace(" > ", $delimiter, $redelimit);   
   }
      
   if($j = $sscat) {
   $vals[$j] = str_replace("->", $delimiter, $redelimit);   
   }
   $j++;
}
unset($redelimit);

 

Here is the current results of the code after it STR_REPLACE the other delimiters to the main delimiter.

 

[1] = VALUE 1

[2] = VALUE 2

[3] = VALUE 3

[4] = VALUE 4|VALUE 5|VALUE 6

[5] = VALUE 7

[6] = VALUE 8|VALUE 9

[7] = VALUE 10

 

Now I just need to repack this array in order like this

[1] = VALUE 1

[2] = VALUE 2

[3] = VALUE 3

[4] = VALUE 4

[5] = VALUE 5

[6] = VALUE 6

[7] = VALUE 7

[8] = VALUE 8

[9] = VALUE 9

[10] = VALUE 10

 

Thanks for your help!

 

Link to comment
https://forums.phpfreaks.com/topic/186965-rework-an-array/#findComment-987320
Share on other sites

Ok I Made some headway.  Setup a test.php to test this out:

 

<?
$columns = 'VID|VENDOR|| > TID > SID|ISBN|COLUMN1|LAST|FIRST|SSID->|COLUMN2';
$del = 1;


if ($del == 3) {
$delimiter = ",";
	} else { if ($del == 2) {
			$delimiter = "\t";
			} else {    
			$delimiter = "|";
	} }

$delimet2 =  ' > TID > SID';
$delimet3 =  ' > TID > SID';
$delimet4 =  'SSID->';
$delim2 = 5;
$delim3 = 5;
$delim4 = 2;

$corecolumns = explode($delimiter,$columns);
$parsecore = array();
$i = 1;


foreach ($corecolumns as $key => $value) {
    $parsecore[$i] = $value;
if(!empty($delimet2)){
if($value == $delimet2) {
	$cat = $i;
}}
if(!empty($delimet3)){
if($value == $delimet3) {
    $scat = $i;
}}
if(!empty($delimet4)){
if($value == $delimet4) {
    $sscat = $i;
}}
$i++;
} //end

echo 'Template: '.implode("|",$corecolumns)."<br>";
unset($corecolumns,$parsecore);

echo 'Other Delimiter Column numbers: '.$cat." ".$scat." ".$sscat.'<br>';

if (!empty($delim2)) {
if ($delim2 == 1) {
$sdelimiter = "|";
} 
if ($delim2 == 2) {
$sdelimiter = "->";
} 
if ($delim2 == 3) {
$sdelimiter = ",";
} 
if ($delim2 == 4) {
$sdelimiter = ">";
} 
if ($delim2 == 5) {
$sdelimiter = " > ";
} 
if ($delim2 == 6) {
$sdelimiter = "~";
} 
if ($delim2 == 7) {
$sdelimiter = "/";
}
if ($delim2 ==  {
$sdelimiter = "\\";
} 
if ($delim2 == 9) {
$sdelimiter = " -> ";
} 
$columns = str_replace($sdelimiter, $delimiter, $columns);	
}

if (!empty($delim3)) {
if ($delim3 == 1) {
$ssdelimiter = "|";
} 
if ($delim3 == 2) {
$ssdelimiter = "->";
} 
if ($delim3 == 3) {
$ssdelimiter = ",";
} 
if ($delim3 == 4) {
$ssdelimiter = ">";
} 
if ($delim3 == 5) {
$ssdelimiter = " > ";
} 
if ($delim3 == 6) {
$ssdelimiter = "~";
} 
if ($delim3 == 7) {
$ssdelimiter = "/";
}
if ($delim3 ==  {
$ssdelimiter = "\\";
} 
if ($delim3 == 9) {
$ssdelimiter = " -> ";
} 
$columns = str_replace($ssdelimiter, $delimiter, $columns);	
}

if (!empty($delim4)) {
if ($delim4 == 1) {
$sssdelimiter = "|";
} 
if ($delim4 == 2) {
$sssdelimiter = "->";
} 
if ($delim4 == 3) {
$sssdelimiter = ",";
} 
if ($delim4 == 4) {
$sssdelimiter = ">";
} 
if ($delim4 == 5) {
$sssdelimiter = " > ";
} 
if ($delim4 == 6) {
$sssdelimiter = "~";
} 
if ($delim4 == 7) {
$sssdelimiter = "/";
}
if ($delim4 ==  {
$sssdelimiter = "\\";
} 
if ($delim4 == 9) {
$sssdelimiter = " -> ";
} 
$columns = str_replace($sssdelimiter, $delimiter, $columns);	
}

echo 'Delimited Template Columns:'.$columns.'<br>';

$columns = explode($delimiter,$columns);

//get the applicable column numbers from Template
$g = 1;
$cnt = 1;
$newcols = array();
$actcnt[$g];
foreach($columns as $colval) {
if(!empty($colval)) {
$newcols[$cnt] = $colval;
$actcnt[$cnt] = $g;
$cnt++;	
}
$g++;
}

echo '<br>Number of Applicable Delimited Columns:'.($cnt -1).'<br>';
echo 'Applicable Template Columns: '.implode('~',$newcols).'<br>';
echo 'Assigned Template Column Number: '.implode('~',$actcnt).'<br>';

//Ok lets Parse the actual RAW Data 
$b = 'VALUE 1|VALUE 2|VALUE 3 > VALUE 4 > VALUE 5|VALUE 6|VALUE 7|VALUE 8|VALUE 9|VALUE 10|VALUE 11->VALUE 12|VALUE 13';
echo 'Raw Values: '.$b.'<br>';

//$b = str_replace($rule, $aid, $b);

$j = 1;
$d = 1;
$vals = array();
foreach(explode($delimiter,$b) as $values) {

if(isset($cat) && $j = $cat) {
foreach(explode($sdelimiter, $values) as $c) {
$vals[$d] = $c;
$d++;
}} else {   
if(isset($scat) && $j = $scat) {
foreach(explode($ssdelimiter, $values) as $c) {
$vals[$d] = $c;
$d++;
}} else {
if(isset($sscat) && $j = $sscat) {
foreach(explode($sssdelimiter, $values) as $c) {
$vals[$d] = $c;
$d++;
} } } } 
$j++;

}
unset($values);

echo 'Parsed Values Against Template: '.implode(",",$vals);

?>

 

Here is the output:

 

Template: VID|VENDOR|| > TID > SID|ISBN|COLUMN1|LAST|FIRST|SSID->|COLUMN2

Other Delimiter Column numbers: 4 4 9

Delimited Template Columns:VID|VENDOR|||TID|SID|ISBN|COLUMN1|LAST|FIRST|SSID||COLUMN2

 

Number of Applicable Delimited Columns:10

Applicable Template Columns: VID~VENDOR~TID~SID~ISBN~COLUMN1~LAST~FIRST~SSID~COLUMN2

Assigned Template Column Number: 1~2~5~6~7~8~9~10~11~13

Raw Values: VALUE 1|VALUE 2|VALUE 3 > VALUE 4 > VALUE 5|VALUE 6|VALUE 7|VALUE 8|VALUE 9|VALUE 10|VALUE 11->VALUE 12|VALUE 13

Parsed Values Against Template: VALUE 1,VALUE 2,VALUE 3,VALUE 4,VALUE 5,VALUE 6,VALUE 7,VALUE 8,VALUE 9,VALUE 10,VALUE 11->VALUE 12,VALUE 13

 

I can't seem to figure out why the code won't process that last bit!

 

Can someone take a look at this last bit of code and hopefully tell me what I am doing wrong?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/186965-rework-an-array/#findComment-987401
Share on other sites

Well might as well post my solution on here.

 

Here is the final code.  Compares a template string with txt string and sorts the data by multipule delimiters:

 

<?
$columns = 'VID|VENDOR|| > TID > SID|ISBN|COLUMN1|LAST|SSID->|FIRST|COLUMN2';
$del = 1;


if ($del == 3) {
$delimiter = ",";
	} else { if ($del == 2) {
			$delimiter = "\t";
			} else {    
			$delimiter = "|";
	} }

$delimet2 =  ' > TID > SID';
$delimet3 =  ' > TID > SID';
$delimet4 =  'SSID->';
$delim2 = 5;
$delim3 = 5;
$delim4 = 2;

$corecolumns = explode($delimiter,$columns);
$parsecore = array();
$i = 1;


foreach ($corecolumns as $key => $value) {
    $parsecore[$i] = $value;
if(!empty($delimet2)){
if($value == $delimet2) {
	$cat = $i;
}}
if(!empty($delimet3)){
if($value == $delimet3) {
    $scat = $i;
}}
if(!empty($delimet4)){
if($value == $delimet4) {
    $sscat = $i;
}}
$i++;
} //end

echo 'Template: '.implode("|",$corecolumns)."<br>";
unset($corecolumns);

echo 'Other Delimiter Column numbers: '.$cat." ".$scat." ".$sscat.'<br>';

if (!empty($delim2)) {
if ($delim2 == 1) {
$sdelimiter = "|";
} 
if ($delim2 == 2) {
$sdelimiter = "->";
} 
if ($delim2 == 3) {
$sdelimiter = ",";
} 
if ($delim2 == 4) {
$sdelimiter = ">";
} 
if ($delim2 == 5) {
$sdelimiter = " > ";
} 
if ($delim2 == 6) {
$sdelimiter = "~";
} 
if ($delim2 == 7) {
$sdelimiter = "/";
}
if ($delim2 ==  {
$sdelimiter = "\\";
} 
if ($delim2 == 9) {
$sdelimiter = " -> ";
} 
$columns = str_replace($sdelimiter, $delimiter, $columns);	
}

if (!empty($delim3)) {
if ($delim3 == 1) {
$ssdelimiter = "|";
} 
if ($delim3 == 2) {
$ssdelimiter = "->";
} 
if ($delim3 == 3) {
$ssdelimiter = ",";
} 
if ($delim3 == 4) {
$ssdelimiter = ">";
} 
if ($delim3 == 5) {
$ssdelimiter = " > ";
} 
if ($delim3 == 6) {
$ssdelimiter = "~";
} 
if ($delim3 == 7) {
$ssdelimiter = "/";
}
if ($delim3 ==  {
$ssdelimiter = "\\";
} 
if ($delim3 == 9) {
$ssdelimiter = " -> ";
} 
$columns = str_replace($ssdelimiter, $delimiter, $columns);	
}

if (!empty($delim4)) {
if ($delim4 == 1) {
$sssdelimiter = "|";
} 
if ($delim4 == 2) {
$sssdelimiter = "->";
} 
if ($delim4 == 3) {
$sssdelimiter = ",";
} 
if ($delim4 == 4) {
$sssdelimiter = ">";
} 
if ($delim4 == 5) {
$sssdelimiter = " > ";
} 
if ($delim4 == 6) {
$sssdelimiter = "~";
} 
if ($delim4 == 7) {
$sssdelimiter = "/";
}
if ($delim4 ==  {
$sssdelimiter = "\\";
} 
if ($delim4 == 9) {
$sssdelimiter = " -> ";
} 
$columns = str_replace($sssdelimiter, $delimiter, $columns);	
}

$columns = explode($delimiter,$columns);

//get the applicable column numbers from Template
$g = 1;
$cnt = 1;
$newcols = array();
$actcnt[$g];
foreach($columns as $colval) {
if(!empty($colval)) {
$newcols[$cnt] = $colval;
$actcnt[$cnt] = $g;
$cnt++;	
}
$g++;
}

echo '<br>Number of Applicable Delimited Columns:'.($cnt -1).'<br>';
echo 'Applicable Template Columns: '.implode('~',$newcols).'<br>';
echo 'Assigned Template Column Number: '.implode('~',$actcnt).'<br>';

//Ok lets Parse the actual RAW Data 
$b = 'VALUE 1|VALUE 2|VALUE 3|VALUE 4 > VALUE 5 > VALUE 6|VALUE 7|VALUE 8 > |VALUE 9|VALUE 10->VALUE 11|VALUE 12|VALUE 13';
echo 'Raw Values: '.$b.'<br>';

//$b = str_replace($rule, $aid, $b);

$j = 1;
$d = 0;
$vals = array();
foreach(explode($delimiter,$b) as $key => $values) {

if(!empty($cat) && $j == $cat) {
foreach(explode($sdelimiter, $values) as $keys => $c) {
$vals[$d] = $c;
$d++;
}} else { if(!empty($scat) && $j == $scat) {
foreach(explode($ssdelimiter, $values) as $keys => $c) {
$vals[$d] = $c;
$d++;
}} else { if(!empty($sscat) && $j == $sscat) {
foreach(explode($sssdelimiter, $values) as $keys => $c) {
$vals[$d] = $c;
$d++;
}} else {	
$vals[$d] = $values;
$d++; 
}}}

$j++;
}


unset($values);

echo 'Parsed Values Against Template: '.implode(",",$vals);

?>

 

Cheers all!

Link to comment
https://forums.phpfreaks.com/topic/186965-rework-an-array/#findComment-987430
Share on other sites

Opps found an error in the last one! This works great:

 

<?
$columns = 'VID|VENDOR|| > TID > SID|ISBN|COLUMN1|LAST|SSID->|FIRST|COLUMN2';
$del = 1;


if ($del == 3) {
$delimiter = ",";
	} else { if ($del == 2) {
			$delimiter = "\t";
			} else {    
			$delimiter = "|";
	} }

$delimet2 =  ' > TID > SID';
$delimet3 =  ' > TID > SID';
$delimet4 =  'SSID->';
$delim2 = 5;
$delim3 = 5;
$delim4 = 2;

$corecolumns = explode($delimiter,$columns);
$parsecore = array();
$i = 1;


foreach ($corecolumns as $key => $value) {
    $parsecore[$i] = $value;
if(!empty($delimet2)){
if($value == $delimet2) {
	$cat = $i;
}}
if(!empty($delimet3)){
if($value == $delimet3) {
    $scat = $i;
}}
if(!empty($delimet4)){
if($value == $delimet4) {
    $sscat = $i;
}}
$i++;
} //end

echo 'Template: '.implode("|",$corecolumns)."<br>";
unset($corecolumns);

echo 'Other Delimiter Column numbers: '.$cat." ".$scat." ".$sscat.'<br>';

if (!empty($delim2)) {
if ($delim2 == 1) {
$sdelimiter = "|";
} 
if ($delim2 == 2) {
$sdelimiter = "->";
} 
if ($delim2 == 3) {
$sdelimiter = ",";
} 
if ($delim2 == 4) {
$sdelimiter = ">";
} 
if ($delim2 == 5) {
$sdelimiter = " > ";
} 
if ($delim2 == 6) {
$sdelimiter = "~";
} 
if ($delim2 == 7) {
$sdelimiter = "/";
}
if ($delim2 ==  {
$sdelimiter = "\\";
} 
if ($delim2 == 9) {
$sdelimiter = " -> ";
} 
$columns = str_replace($sdelimiter, $delimiter, $columns);	
}

if (!empty($delim3)) {
if ($delim3 == 1) {
$ssdelimiter = "|";
} 
if ($delim3 == 2) {
$ssdelimiter = "->";
} 
if ($delim3 == 3) {
$ssdelimiter = ",";
} 
if ($delim3 == 4) {
$ssdelimiter = ">";
} 
if ($delim3 == 5) {
$ssdelimiter = " > ";
} 
if ($delim3 == 6) {
$ssdelimiter = "~";
} 
if ($delim3 == 7) {
$ssdelimiter = "/";
}
if ($delim3 ==  {
$ssdelimiter = "\\";
} 
if ($delim3 == 9) {
$ssdelimiter = " -> ";
} 
$columns = str_replace($ssdelimiter, $delimiter, $columns);	
}

if (!empty($delim4)) {
if ($delim4 == 1) {
$sssdelimiter = "|";
} 
if ($delim4 == 2) {
$sssdelimiter = "->";
} 
if ($delim4 == 3) {
$sssdelimiter = ",";
} 
if ($delim4 == 4) {
$sssdelimiter = ">";
} 
if ($delim4 == 5) {
$sssdelimiter = " > ";
} 
if ($delim4 == 6) {
$sssdelimiter = "~";
} 
if ($delim4 == 7) {
$sssdelimiter = "/";
}
if ($delim4 ==  {
$sssdelimiter = "\\";
} 
if ($delim4 == 9) {
$sssdelimiter = " -> ";
} 
$columns = str_replace($sssdelimiter, $delimiter, $columns);	
}

$columns = explode($delimiter,$columns);

//get the applicable column numbers from Template
$g = 1;
$cnt = 1;
$newcols = array();
//$actcnt[$g];
foreach($columns as $colval) {
if(!empty($colval)) {
$newcols[$cnt] = $colval;
$actcnt[$cnt] = $g;
$cnt++;	
}
$g++;
}

echo '<br>Number of Applicable Delimited Columns:'.($cnt -1).'<br>';
echo 'Applicable Template Columns: '.implode('~',$newcols).'<br>';
echo 'Assigned Template Column Number: '.implode('~',$actcnt).'<br>';

//Ok lets Parse the actual RAW Data 
$b = 'VALUE 1|VALUE 2|VALUE 3|VALUE 4 > VALUE 5 > VALUE 6|VALUE 7|VALUE 8 > |VALUE 9|VALUE 10->VALUE 11|VALUE 12|VALUE 13';
echo 'Raw Values: '.$b.'<br>';

//$b = str_replace($rule, $aid, $b);

$j = 1;
$d = 1;
$vals = array();
foreach(explode($delimiter,$b) as $key => $values) {

if(!empty($cat) && $j == $cat) {
foreach(explode($sdelimiter, $values) as $keys => $c) {
$vals[$d] = $c;
$d++;
}} else { if(!empty($scat) && $j == $scat) {
foreach(explode($ssdelimiter, $values) as $keys => $c) {
$vals[$d] = $c;
$d++;
}} else { if(!empty($sscat) && $j == $sscat) {
foreach(explode($sssdelimiter, $values) as $keys => $c) {
$vals[$d] = $c;
$d++;
}} else {	
$vals[$d] = $values;
$d++; 
}}}

$j++;
}


unset($values);

echo 'Parsed Values Against Template: '.implode(",",$vals).'<br>';

//repack the variables
$x = 0;
$parsevals = array();
foreach($actcnt as $restuff) {

$parsvals[$x] = $vals[$restuff];
$x++;
}

echo 'Parsed Values Against Template: '.implode(",",$parsvals).'<br>';
//echo the example


$x = 1;
foreach($parsvals as $key => $value) {

echo 'cat: '.$newcols[$x].'   vals: '.$value.'<br>';
$x++;
}


?>

Link to comment
https://forums.phpfreaks.com/topic/186965-rework-an-array/#findComment-987435
Share on other sites

You got the column count wrong, you had an extra in their somewhere.

instead of trying to figure out the code, just made a new routine, to do this

It was quite a challenge trying to figure it out, but it looks like from your code

that you already know the sub-delimiters of a column.

so that made pretty easy work afterwards

<?php
$columns = 'VID|VENDOR|| > TID > SID|ISBN|COLUMN1|LAST|FIRST|SSID->|COLUMN2';
$delims = array(',','\t','|',' > ','->','~','/','\\',);

$sdels= array(
    array(null,2),  // apply to whole, seperate by delimeter '|' (2)
    array(3,3),     // apply to column 3, delimter ' > ' (3)
    array(8,4)      // apply to column 8, delimiter '->' (4)
);

foreach($sdels as $dinfo)
{
    if($dinfo[0]==null)
    {
        $cols=explode($delims[$dinfo[1]],$columns);
    } else {
        $cols[$dinfo[0]]=explode($delims[$dinfo[1]],$cols[$dinfo[0]]);
    }
}

header('Content-Type: text/plain');
var_dump($cols);
$b = 'COL 0|COL 1|COL 2|COL 3 SUB 1 > COL 3 SUB 2 > COL 3 SUB 3|COL 4|COL 5|COL 6|COL 7|COL 8 SUB 1->COL 8 SUB 2|COL 9';
foreach($sdels as $dinfo)
{
    if($dinfo[0]==null)
    {
        $vars=explode($delims[$dinfo[1]],$b);
    } else {
        $vars[$dinfo[0]]=explode($delims[$dinfo[1]],$vars[$dinfo[0]]);
    }
}
$idx=0;
foreach($vars as $var)
{
    if(is_string($var))
    {
        $vals[$idx++]=$var;
    } else {
        foreach($var as $val)
            $vals[$idx++]=$val;
    }
}
var_dump($vals)
?>

Link to comment
https://forums.phpfreaks.com/topic/186965-rework-an-array/#findComment-987440
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.