The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

PhP coding - A question which is likely obvious to anyone who has done this a lot

tbloxhamtbloxham Registered User regular
edited October 2010 in Help / Advice Forum
Hey all

So, I'm doing some coding for a website I maintain at work, and usually it's just simple stuff but I want to add a new feature. It requires me to be able to take the list of numbers which look like this in an external txt file...

4 100 1
5 104 1
6 11 1
15 109 1

and convert them in php to be....

$test = array();
$test[4][100]=1;
$test[5][104]=1;
$test[6][11]=1;
$test[15][109]=1;

Any thoughts? I'm sure it should be simple, it would certainly be trivial in C or Fortran.

edit - Oh, and the numbers in the external file are seperated by tabs, at least for now. Primarily I just need a way that I can have a place to save some variables in a logical place which are created during a session using the website, and then load them back in when you come back to it.

"That is cool" - Abraham Lincoln
tbloxham on

Posts

  • Jimmy KingJimmy King Registered User regular
    edited October 2010
    I would look at these two functions:
    http://php.net/manual/en/function.explode.php
    http://php.net/manual/en/function.fgets.php

    Basically, read the file line by line. Split each line into its own little array. Use that array to populate each row of your final array.

    Or you could use sqlite to store that info between sessions.

    Jimmy King on
  • SeñorAmorSeñorAmor !!! Registered User regular
    edited October 2010
    I would use file_get_contents() to read the file. Then I would use explode() with a newline (\n) as a delimiter to create an array whose elements are each of the lines of the file. Finally, I would use a combination of list() and explode() within a foreach block to separate the individual items and use the resulting variables to populate your array.

    I can write the code for you, but I'm hesitant to in case this is homework or something.

    *Edit, reading further, I see this is for a website you're working on and not homework so here you go:
    <?php
    
    // Path to file
    $filename = '/path/to/file';
    
    // Open file and save contents
    $contents = file_get_contents($filename) or die('Cannot open file!');
    
    // Split file contents into array based on newline
    $array = explode("\n", $contents);
    
    // Loop over array elements
    foreach ($array as $k => $v)
    {
    	// Assign variables split on tabs
    	list($one, $two, $three) = explode("\t", $v);
    	
    	// Assign data to big array
    	$newarray[$one][$two] = $three;
    }
    
    ?>
    

    SeñorAmor on
  • tbloxhamtbloxham Registered User regular
    edited October 2010
    Thanks a lot! Now my excluded samples list will be much easier to make and I won't need to write php pipeline code.

    tbloxham on
    "That is cool" - Abraham Lincoln
Sign In or Register to comment.