我有一个PHP应用程序,它在输入中使用CSV;在第一列中,我将类别和子类别分为“|”。我必须把这些东西放在一个Open Cart DB中,它有Category和Subcategory相同的表(列“parent_id”表示Category的category_id)。 我想要构建一个包含所需字段的类,如下所示:
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;
}
然后我分析数据:
$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;
}
我应该有一个数组,里面填充了一系列独特的对象,如:
// OUT(3)
Category 1
Subcategory 1
Category 2
Subcategory 2...
如果我在(1)和(2)中放入$ categoria(不是$ categorie)的回声,我会看到我在OUT(3)中记下的确切内容,这让我认为我的“引擎”是正确的。当我尝试搜索数组时会出现问题:
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>";
因为我根本没有获得类别,而且大多数但并非所有子类别。 我在使用“搜索引擎”做错了什么,或者我误解了PHP Arrays的某些东西(可能是因为我来自C ++而且这个PHP是我的脑海中的一部分)?
答案 0 :(得分:0)
我解决了问题,或更好:问题。
1)我拼错了变量:在(2)中我写道:
$categorie[$colonne[1]] = $categorie;
正确的写作是:
$categorie[$colonne[1]] = $categoria;
2)更大的问题来自于我对PHP的“不知情”,实际上修复了问题1,我得到了如下输出:
subcategory1
subcategory1
subcategory2
subcategory2
等等。这导致我在做的时候猜测:
$categorie[$colonne[1]] = $categoria;
PHP不会复制里面的值,而是使用地址。遵循正确的代码:
switch ( $el_corr )
{
case 0: // Categorie e sottocategorie
$colonne = explode( "|", $riga_file[$el_corr] );
$categoria = new Categorie();
$categoria->image = "";
$categoria->top = 1;
$categoria->column = 1;
$categoria->sort_order = 0;
$categoria->status = 1;
// Category_Description Campi
$categoria->language_id = 1;
$categoria->name = $colonne[0];
$categoria->description = "";
$categoria->meta_description = "";
$categoria->meta_keywords = "";
////////////////////////
if( $categorie[$colonne[0]] == NULL )
//if( !trova_categorie( $colonne[0], $categorie ) )
{
$categoria->name = $colonne[0];
$categoria->category_id = $id_categoria;
$categoria->parent_id = 0;
$categorie[$colonne[0]] = $categoria;
$id_categoria++;
}
$categoria = new Categorie();
$categoria->image = "";
$categoria->top = 1;
$categoria->column = 1;
$categoria->sort_order = 0;
$categoria->status = 1;
// Category_Description Campi
$categoria->language_id = 1;
$categoria->name = $colonne[0];
$categoria->description = "";
$categoria->meta_description = "";
$categoria->meta_keywords = "";
if( $colonne[1] != NULL )
{
$categoria->name = $colonne[1];
if( $categorie[$colonne[1]] == NULL )
//if( !trova_categorie( $colonne[1], $categorie ) )
{
$categoria->category_id = $id_categoria;
$categoria->parent_id = $categorie[$colonne[0]]->category_id;
$categorie[$colonne[1]] = $categoria;
$id_categoria++;
}
}
break;
}