Re: list() Struktur auslagern [message #176756 is a reply to message #176700] |
Sun, 22 January 2012 06:19 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
Senior Member |
|
|
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(at)news(dot)demon(dot)co(dot)uk> (2004)
|
|
|