user2632918
user2632918

Reputation:

Multidimensional Array filled from textfile

i would like to import a txt-file with datas for a project. The file have different sections.

$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);

But the articels are not added right to the multi-array, because the loop for the query creates only one insert, but when i make a var_dump of of the $object, there looks that the values all be inserted to the array.

I think i have a logic error with filling the array and reading it out.

echo count($object['artikel']) . "\n";die();

This give me the result, that only one Element in the array, but normaly it have to be over 800.

The Element in the array is the last one from the hole Articel list.

Upvotes: 0

Views: 63

Answers (2)

Barmar
Barmar

Reputation: 780724

Change:

$object["artikel"] = array(
    $articeldetails
);

to:

$object["artikel"][] = $articeldetails;

You're not adding to $object['artikel'] each time, you're overwriting it.

Upvotes: 0

ylerjen
ylerjen

Reputation: 4249

You are replacing the same variable each time you go in the while loop...

$object['artikel'] = array($articeldetails);

With this affectation, the array $object has the same cell ('artikel') replaced everytime...

Upvotes: 0

Related Questions