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


Groups > comp.lang.php > #4566

Re: list() Struktur auslagern

Message-ID <1921912.G2gmEF7JIg@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-01-22 07:19 +0100
Subject Re: list() Struktur auslagern
Newsgroups comp.lang.php
References <4f16f4b4$1_1@news.tiscalinet.ch> <4f17210e$0$6931$e4fe514c@news2.news.xs4all.nl>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


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)

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web