Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #4509 > unrolled thread

list() Struktur auslagern

Started by"Maximilian Koch" <xlarge@vtxmail.ch>
First post2012-01-18 17:34 +0100
Last post2012-01-22 07:00 +0100
Articles 4 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  list() Struktur auslagern "Maximilian Koch" <xlarge@vtxmail.ch> - 2012-01-18 17:34 +0100
    Re: list() Struktur auslagern Erwin Moller <erwinmollerusenet@xs4all.nl> - 2012-01-18 20:44 +0100
      Re: list() Struktur auslagern Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-22 07:19 +0100
    Re: list() Struktur auslagern Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-22 07:00 +0100

#4509 — list() Struktur auslagern

From"Maximilian Koch" <xlarge@vtxmail.ch>
Date2012-01-18 17:34 +0100
Subjectlist() Struktur auslagern
Message-ID<4f16f4b4$1_1@news.tiscalinet.ch>
Hallo NG

ich möchte aus einer CSV datei die inhalte auslesen, diese können sich 
jedoch mal ändern in der reihenfolge. und es wird danach umständlich an 
mehreren orten im PHP Script die sache zu ändern..

gäbe es nicht eine möglichkeit diese auszulegen..

in etwa so:
------------------------------

$datenstruktur = "$land, $artnrCH, $artnrAT, $artnrDE, $rezepturnummer, 
$artikel, $reichweiteintagen, $reichweitebismbintagen, $istbestand, 
$planPPS, $reservation, $planrwbismbintagen, $mindestbestand, $netogewicht, 
$me, $kostenstelle, $MHD, $vepal, $gebch, $chrez, $schnitt3M, $schnitt6M, 
$schnitt12M, $schnitttotal";

foreach ($datenbank as $datenbank_temp)
            {
             list($dispoliste_datenstruktur) = explode(";", 
$datenbank_temp);
            }
----------------------------------------------

allerdings funktioniert das so nicht. kann mir jemand weiterhelfen?

Danke 

[toc] | [next] | [standalone]


#4510

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2012-01-18 20:44 +0100
Message-ID<4f17210e$0$6931$e4fe514c@news2.news.xs4all.nl>
In reply to#4509
On 1/18/2012 5:34 PM, Maximilian Koch wrote:
> Hallo NG
>
> ich möchte aus einer CSV datei die inhalte auslesen, diese können sich
> jedoch mal ändern in der reihenfolge. und es wird danach umständlich an
> mehreren orten im PHP Script die sache zu ändern..
>
> gäbe es nicht eine möglichkeit diese auszulegen..
>
> in etwa so:
> ------------------------------
>
> $datenstruktur = "$land, $artnrCH, $artnrAT, $artnrDE, $rezepturnummer,
> $artikel, $reichweiteintagen, $reichweitebismbintagen, $istbestand,
> $planPPS, $reservation, $planrwbismbintagen, $mindestbestand, $netogewicht,
> $me, $kostenstelle, $MHD, $vepal, $gebch, $chrez, $schnitt3M, $schnitt6M,
> $schnitt12M, $schnitttotal";
>
> foreach ($datenbank as $datenbank_temp)
>              {
>               list($dispoliste_datenstruktur) = explode(";",
> $datenbank_temp);
>              }
> ----------------------------------------------
>
> allerdings funktioniert das so nicht. kann mir jemand weiterhelfen?
>
> Danke
>
>


[In English: I can read German a little, but not produce it.]

You command:
explode(";",$datenbank_temp);
will explode on ;

For example:
$myPartsArray = explode(",","eins,zwei,drei");

Now you have 3 elements in your array:
$myPartsArray[0] <-- "eins"
$myPartsArray[1] <-- "zwei"
$myPartsArray[2] <-- "drei"


Looking at your example you want to explode on "," and not on ";".

Also: You use list(), which makes no sense like that.
Explode() already returns an array, so you can just do:

$dispoliste_datenstruktur = explode(",",$datenbank_temp);

That way you'll have all in the array $dispoliste_datenstruktur.

You can always check what is inside some array quickly using the handy 
function print_r(), like this:

echo "<pre>";
print_r($dispoliste_datenstruktur);
echo "</pre>";

In case I didn't answer your question at all, which is very possible, 
just ask again. :-)

Regards,
Erwin Moller

-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

[toc] | [prev] | [next] | [standalone]


#4566

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-01-22 07:19 +0100
Message-ID<1921912.G2gmEF7JIg@PointedEars.de>
In reply to#4510
Erwin Moller wrote:

> On 1/18/2012 5:34 PM, Maximilian Koch wrote:
>> ich möchte aus einer CSV datei die inhalte auslesen, diese können sich
                                                              ^^^^^^^^^^^
>> jedoch mal ändern in der reihenfolge. und es wird danach umständlich an
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> mehreren orten im PHP Script die sache zu ändern..
>>
>> gäbe es nicht eine möglichkeit diese auszulegen..
>> […]
> [In English: I can read German a little, but not produce it.]
> 
> You command:
> explode(";",$datenbank_temp);
> will explode on ;
> 
> For example:
> $myPartsArray = explode(",","eins,zwei,drei");
> 
> Now you have 3 elements in your array:
> $myPartsArray[0] <-- "eins"
> $myPartsArray[1] <-- "zwei"
> $myPartsArray[2] <-- "drei"
> 
> 
> Looking at your example you want to explode on "," and not on ";".
> 
> Also: You use list(), which makes no sense like that.
> Explode() already returns an array, so you can just do:
> 
> $dispoliste_datenstruktur = explode(",",$datenbank_temp);
> 
> That way you'll have all in the array $dispoliste_datenstruktur.

That is correct, but beside the point, as the question was how to get rid of 
the numeric indexes that are hard to maintain.  The list() approach is 
viable if list() is given more than one argument, one for each component of 
the comma-separated value:

| > list($foo, $bar) = explode(',', 'baz,bla');
| > echo implode('#', array($foo, $bar));
| baz#bla

IOW, now $foo and $bar can be used in place of $dispoliste_datenstruktur[0] 
and $dispoliste_datenstruktur[1], respectively.  The drawback of this 
approach is that all fields need to be present or a notice will be 
generated:

$ php -a
Interactive mode enabled

<?php

list($foo, $bar) = explode(',', 'baz');
echo implode('#', array($foo, $bar));
PHP Notice:  Undefined offset: 1 in - on line 3
PHP Stack trace:
PHP   1. {main}() -:0
baz#

Fortunately, list() appears to be ignoring too many elements RHS.

$ php -a
Interactive mode enabled

<?php

list($foo, $bar) = explode(',', 'foo,bar,baz');
echo implode('#', array($foo, $bar));
foo#bar

> In case I didn't answer your question at all, which is very possible,
> just ask again. :-)

You did not answer the question at all.  I have marked the relevant part 
that you overlooked.


PointedEars
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
  -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

[toc] | [prev] | [next] | [standalone]


#4565

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-01-22 07:00 +0100
Message-ID<60348188.5pjnEbAIS9@PointedEars.de>
In reply to#4509
Maximilian Koch wrote:

> ich m�chte aus einer CSV datei die inhalte auslesen, diese k�nnen sich
> jedoch mal �ndern in der reihenfolge. und es wird danach umst�ndlich an
> mehreren orten im PHP Script die sache zu �ndern..

fgetcsv() und auch Dein Ansatz liefern Dir ein Array.  Du kannst *einmalig* 
den Wert eines Array-Elements einer Variablen zuweisen, und dann im Script 
die Variable verwenden.  Alternativ kannst Du *einmalig* Konstanten oder 
Eigenschaften definieren, welche als Wert den jeweiligen Array-Index haben 
(ich empfehle hier einen objektorientierten Ansatz mit Klassenkonstanten, 
oder Eigenschaften, wenn es flexibler sein muss), und dann verwendest Du die 
Konstante oder Eigenschaftsreferenz als Array-Index.  Zum Beispiel so 
(ungetestet, aber in dieser Form bewährt):

class Import
{
  protected $_fields = array(
    0 => 'country',
    1 => 'sku_ch'
  );

  public function import($file)
  {
    $mapper = ArticleMapper::getInstance();

    $mapper->startTransaction();

    while (($data = fgetcsv($file)) !== false)
    {
      for ($i = count($data); $i--;)
      {
        $data[$this->_fields[i]] = $data[i];
      }

      $article = new Article($data);

      try
      {
        $mapper->save($article);
      }
      catch (Exception $e)
      {
        $mapper->rollBack();
        break;
      }
    }

    if ($data === false && !$mapper->commit())
    {
      $mapper->rollBack();
    }
  }
}

> g�be es nicht eine m�glichkeit diese auszulegen..

"auszulegen"?  Bitte erst lesen, dann denken, dann posten.

BTW: de.comp.lang.php existiert.

BTW2: Deine Umlaute sind kapott, lies bitte vor weiteren Postings 
<http://oe-faq.de/>.


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web