Page 1 of 1

naming array based on file name

Posted: Wed Aug 30, 2006 2:52 pm
by peterpal
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'd like to bring in a csv file and parse it into an array.  That I can do, but I'd like to call the array the same as thing as the file using the variable ($file in this case) that I am using to store the file name so that I don't actually have to know the file name (other than when I assigned to $file).

Code: Select all

$current_row = 1;
$file = 'temp';
$handle = fopen($file, 'r');

while ( ($data = fgetcsv($handle, 10000, ',') ) !== FALSE )
{
   $number_of_fields = count($data);
   if ($current_row == 1)
   {
   //Header line
       for ($c=0; $c < $number_of_fields; $c++)
       {
           $header_array[$c] = $data[$c];
       }
   }
   else
   {
   //Data line
       for ($c=0; $c < $number_of_fields; $c++)
       {
           $reg[$header_array[$c]][$current_row-2] = $data[$c];
       }
   }
   $current_row++;
}

echo "<PRE>";
print_r ($reg);

echo "</PRE>";
but instead of calling the big matrix something else (in the case $reg) I want to call it the same thing as the file name (in this case temp) so my print_r statement would be ($temp) not ($reg). Is this possible?

If that's possible can I then do the print_r statement again using the $file somehow and not knowing the file name at that point in time?

Thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Aug 31, 2006 7:00 pm
by RobertGonzalez
Are you talking about variable variables?

Code: Select all

<?php
/* Declare and value the var $file */
$file = 'temp';

/* Use the declared value of $file as the name of the array */
${$file} = array();
?>
This is untested, but try it and see what happens.