Php 从文本文件填充多维数组
我想为一个项目导入一个包含数据的txt文件。该文件有不同的部分Php 从文本文件填充多维数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我想为一个项目导入一个包含数据的txt文件。该文件有不同的部分 $file = fopen( $filename, "r" ); $object = array(); while( !feof( $file ) ) { $line = fgets($file); $items = explode( ";", $line ); //Import the global values if( count($ite
$file = fopen( $filename, "r" );
$object = array();
while( !feof( $file ) )
{
$line = fgets($file);
$items = explode( ";", $line );
//Import the global values
if( count($items) == 1 )
{
$arraySection = str_replace("#", "",trim($items[0]) );
}
else if ( count($items) == 3 )
{
$object[$arraySection][$items[0]] = $items[1];
}
else if ( count($items) == 4 )
{
//$object[$arraySection][] = array("gewerknummer" => $items[1], "gewerkbezeichnung" => $items[2]);
}
//Import the articel
else if ( count($items) > 4 )
{
if( $items[0] == "Artikel-Nr" )
{
$articelrow = $items;
}
else
{
$articeldetails = array();
for($i = 0; $i < count($items)-1; $i++)
{
$articeldetails[] = array($articelrow[$i]=>$items[$i]);
}
$object["artikel"] = array(
$articeldetails
);
}
}
}
//Start of import the array to the database
$arrValue = array();
for($i = 0; $i < count($object['artikel']); $i++)
{
$tArray = array();
$tArray[] = $object['Objekt']['Objektnr'];
$tArray[] = $object['SuAdresse']['SUNr'];
$tArray[] = $object['artikel'][$i][0]['Artikel-Nr'];
$tArray[] = "'" . $object['artikel'][$i][1]['Artikel']. "'";
$tArray[] = "'" . $object['artikel'][$i][2]['Beschreibung'] . "'";
$tArray[] = "'" . $object['artikel'][$i][3]['Einheit'] . "'";
$tArray[] = "'" . str_replace(",", ".", $object['artikel'][$i][4]['Preis-Pro-Einheit']) . "'";
$tArray[] = $object['artikel'][$i][5]['AnzahlParameter'];
$tArray[] = "'" . $object['artikel'][$i][6]['P1_Einheit'] . "'";
$tArray[] = "'" . $object['artikel'][$i][7]['P2_Einheit'] . "'";
$tArray[] = "'" . $object['artikel'][$i][8]['P3_Einheit'] . "'";
$tArray[] = "'" . $object['artikel'][$i][9]['MBS Artikel'] . "'";
$tArray[] = $object['artikel'][$i][10]['Status'];
$tArray[] = $object['artikel'][$i][11]['SuArtikel'];
$tArray[] = $object['artikel'][$i][12]['SuGewerke'];
$tArray[] = "'" . $object['artikel'][$i][13]['PreisStatus'] . "'";
if ( empty( $object['artikel'][$i][14]['ZulageMindermengenArtikelNr'] ) )
$tArray[] = 0;
else
$tArray[] = 0 . $object['artikel'][$i][14]['ZulageMindermengenArtikelNr'];
$arrValue[] = "(" . implode(",", $tArray) . ")";
}
$query = "INSERT INTO objekt_artikel
(
id_objekt,
id_subunternehmer,
artikelnummer,
artikel,
beschreibung,
einheit,
preis_pro_einheit,
anzahl_parameter,
p1_einheit,
p2_einheit,
p3_einheit,
mbs_artikel,
status,
su_artikel,
su_gewerk,
preis_status,
zulage_mindermengen_artikel_nummer
)
VALUES " . implode(",", $arrValue);
这给了我一个结果,数组中只有一个元素,但通常它必须超过800
数组中的元素是hole Articel列表中的最后一个元素。每次进入while循环时,都要替换相同的变量
$object['artikel'] = array($articeldetails);
通过这种矫揉造作,数组$object每次都会替换相同的单元格('artikel')。更改:
$object["artikel"] = array(
$articeldetails
);
致:
您不是每次都要添加到$object['artikel']
中,而是要覆盖它
$object["artikel"] = array(
$articeldetails
);
$object["artikel"][] = $articeldetails;