http//yahoo.com
Joe
http//google.com
http://ebay.com
Dave
http//foo.com
http//facebook.com
http//stackoverflow.com
Vincent
http//example.com
http//php.net
Terry
http//apple.com
data.txt goes on with hundreds of names and links in similar format.
ok... I have a text data file. I want to use PHP to put the name as one element and combine the link(s) between the names into one element rather than each link a separate element in an array. I tried to do it but got stuck.
here's what i have so far.
<?php
$data = file_get_contents('data.txt');
$data_exploded = explode ("\n",$data);
$arraysize = count($data_exploded);
$z=0;
$newarray = array();
$namearr = array();
$linksarr = array();
$finalarr = array();
$numlink =array(); //array that stores number of links btwn names
echo "array size $arraysize <br />";
var_dump($data_exploded);
//index interval between the file names
for($i=0; $i<$arraysize; $i++){
//check if it is a name
if(substr($data_exploded[$i], 0, 4)!=="http"){
$name =$i;
array_push($newarray,$name); //the index number of names store in this array
}
}
$i_i=0;
$newarray_size = count($newarray);
//calculate the number of links btwn the name intervals
for($i_i=0; $i_i< $newarray_size-1; $i_i++){
$link = $newarray[$i_i+1]-$newarray[$i_i]-1;
array_push($numlink,$link); //store the number of links in this array
}
echo $maxnumlink = max($numlink); //the high number of links in the array
//loop through the main array
for($j=0; $j<$arraysize; $j++){
if(in_array($j, $newarray)){ //check to see if it is a name
array_push($finalarr,$data_exploded[$j]);
}else{
$z = $j + $maxnumlink;
for($k=$j; $k<=$z; $k++){
if(substr($data_exploded[$k], 0, 4)!=="http"){
break;
}
array_push($finalarr,$data_exploded[$k]);
}
}
}
?>

New Topic/Question
Reply



MultiQuote







|