I have a PHP application that takes a CSV in input; in the first column I have both category and subcategory splitted by a '|'. I have to put this stuff in a Open Cart DB, which has the same table for Category and Subcategory (a column "parent_id" indicates the category_id of the Category). I thought to build a class with all the fields needed as follows:
class Categorie {
public $category_id;
public $parent_id;
public $image;
public $top;
public $column;
public $sort_order;
public $status;
public $date_modified;
public $date_added;
public $language_id;
public $name;
public $description;
public $meta_description;
public $meta_keywords;
}
Then I analyze the data:
$categorie = array();
while ( ( $riga_file = fgetcsv( $file, 100000, "\t" ) ) !== false )
{
$array_el = count( $riga_file );
for( $el_cur = 0; $el_cur < $array_el; $el_cur++ )
{
switch ( $el_cur )
{
case 0:
$colonne = explode( "|", $riga_file[$el_corr] );
$categoria = new Categorie();
$categoria->image = "";
$categoria->top = 1;
$categoria->column = 1;
// ... bla adding description data
if( $categorie[$colonne[0]] == NULL ) // (1)
{
$categoria->name = $colonne[0];
$categoria->category_id = $id_categoria;
$categoria->parent_id = 0;
$categorie[$colonne[0]] = $categoria;
$id_categoria++;
}
if( $colonne[1] != NULL ) // (2)
{
$categoria->name = $colonne[1];
if( $categorie[$colonne[1]] == NULL )
{
$categoria->category_id = $id_categoria;
$categoria->parent_id = $categorie[$colonne[0]]->category_id;
$categorie[$colonne[1]] = $categorie;
$id_categoria++;
}
}
break;
}
I should have an array filled with a collection of unique objects like:
// OUT(3)
Category 1
Subcategory 1
Category 2
Subcategory 2...
If I put echoes of $categoria (not $categorie) inside (1) and (2), I see exactly what I wrote down in OUT(3), which leads me to think that my "engine" is correct. The problems come when I try to search into the array:
echo "<table border='1'>";
foreach( $categorie as $value )
{
echo "<tr>
<td>$value->category_id</td>
<td>$value->parent_id</td>
<td>$value->name</td>
</tr>";
}
echo "</table>";
because I don't get Categories at all, and most but not all Subcategories. Am I doing something wrong with "search engine" or I misunderstood something with PHP Arrays (probably because I come from C++ and this php is f**king my mind)?