Create array from dynamic variables PHP -
i working following code. every time line ready form file, need add associative array
$fp = fopen("printers.txt", "r"); // open ptinters.txt read fgets() // while not end of file, read line , store in $printer while (!feof($fp)) { $printer = fgets($fp, 256); // split line of text 3 sections , store them // variables named $pname $printertype , $numpages. $temparray = explode(":", $printer); $pname = $temparray[0]; $printertype = $temparray[1]; $numpages = $temparray[2]; //create 2 arrays. first stores $pname , $printertype // second stores $pname , $numpages }; // close while !feof $fp loop. fclose($fp); // close $fp file pointer stream.
the following code creates array1 = [name] => printertype
, array2 = [name] => numpages
.
$array1 = array(); $array2 = array(); $fp = fopen("printers.txt", "r"); // open ptinters.txt read fgets() while (!feof($fp)) { $printer = fgets($fp, 256); $temparray = explode(":", $printer); $array1[$temparray[0]] = $temparray[1]; $array2[$temparray[0]] = $temparray[2]; } fclose($fp);
if have duplicate printer names following gives array1 array{[0] => array([name] => printertype),...[n] => array([name] => printertype)}
, array2 array{[0] => array([name] => numpages),...[n] => array([name] => numpages)}
$i = 0; while (!feof($fp)) { $printer = fgets($fp, 256); $temparray = explode(":", $printer); $array1[$i] = array($temparray[0] => $temparray[1]); $array2[$i] = array($temparray[0] => $temparray[2]); $i++; }
based on comments:
$ptype = array(); $pages = array(); $fp = fopen("printers.txt", "r"); // open ptinters.txt read fgets() while (!feof($fp)) { $printer = fgets($fp, 256); $temparray = explode(":", $printer); $pname = $temparray[0]; $printertype = $temparray[1]; $numpages = $temparray[2]; $ptype[$pname] = $printertype; $pages[$pname] = $numpages; } fclose($fp);
Comments
Post a Comment