I have the article class with several private vars. Then I’m trying to open old DBF database (converted to CSV), and fill some of fields from CSV to my $article.
As I have 4747 articles in CSV file, I’m trying to create an array of articles.
How ever, if I echo my articles from array right after they are written into it, it looks good. However, when while loop is closed, my array seams to be filled with empty rows, because count will show exactly 4747 members of array, but non of them can bi seen.
Looks like array is actually a pointer to $columns, and as soon as while loop is ended, I’m pointing to nothing, and that’s why I get nothing ? 🙂
How can I force (if I can) PHP to write exact data into array of objects ?
public function readCSVFile ($article)
{
$row=0;
$singlecolumn='';
$columns = '';
$articlesarray = array();
//$columns = array();
//echo $current_file_path;// $this->csvtoopen = $_POST['csvtoimport'];
if (($handle = fopen("e:\\Tmp\\DBF2CSV\\tartikli.csv", "r")) != FALSE)
{
while (!feof($handle))
{
$columns = fgetcsv($handle, 1000, ",");
$article->id=$columns[0];
$article->name=$columns[1];
$article->unit=$columns[2];
$article->price=$columns[14];
$article->stack=$columns[20];
$articlesarray[$row]=$article;
//echo $articlesarray[$row]->name; //---> Works fine
$row++;
}
}
echo count($articlesarray);
for ($i=0;$i<count($articlesarray);$i++)
{
echo $articlesarray[$i]->name; //----> does not work any more
}
fclose($handle);
return $articlesarray;
}
You need to create a new instance of $article for each iteration of array and in that case there is no point in passing the $article as parameter.
Just do something like following