Autor Beitrag
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Di 31.05.05 19:48 
Moin... ;)

Ne kleine kurze PHP-Frage: Wie kann ich in einem Array zwei Werte tauschen?
Beispiel:
ausblenden Quelltext
1:
2:
3:
4:
$myarray[0] = "peter";
$myarray[1] = "hans";
$myarray[2] = "wurst";
tauschen($myarray, 0, 2);

Ich tausche also in $myarray die Werte von 0 und 2. Danach sieht das Array also so aus:
$myarray[0] :arrow: wurst
$myarray[1] :arrow: hans
$myarray[2] :arrow: peter

Danke schonmal. ;)
Malo05
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 34

Windows XP Professional SP2
D5 Prof (SSL), D7 Pers, D2005 Pers
BeitragVerfasst: Di 31.05.05 19:51 
Du könntest ein zweites Array erstellen, den Inhalt des ersten da "reinkopieren" und dann die Änderungen am 2. Array vornehmen. Danach eventuell wieder alles im ersten Array speichern.
WeBsPaCe Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Di 31.05.05 19:55 
Mhmm.. Das ist mir schon klar. Ich kann mir auch ne eigene Funktion schreiben. Aber ich würde gerne eine Funktion aus der PHP-Bibliothek nehmen, wenn da eine drin ist. ;)
Summerhill
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 23



BeitragVerfasst: Di 31.05.05 19:57 
schau doch nach

php.net

_________________
Summerhill!
WeBsPaCe Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Di 31.05.05 19:59 
user profile iconSummerhill hat folgendes geschrieben:
schau doch nach
php.net

Was meinst du, was ich die ganze Zeit mache? Aber ich finde nichts! :roll: Jetzt bin ich dazu übergegangen, alle Array-Funktionen durchzusehen.
WeBsPaCe Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Di 31.05.05 20:07 
Es geht um genau so eine Funktion da:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
function SwitchArrayValues($FUNCTION_ARRAY, $FUNCTION_KEY1, $FUNCTION_KEY2)
  {
  $FUNCTION_TEMPVALUE = $FUNCTION_ARRAY[$FUNCTION_KEY2];
  $FUNCTION_ARRAY[$FUNCTION_KEY2] = $FUNCTION_ARRAY[$FUNCTION_KEY1];
  $FUNCTION_ARRAY[$FUNCTION_KEY1] = $FUNCTION_TEMPVALUE;
  RETURN($FUNCTION_ARRAY);
  }


Beispiel:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
<?php
function SwitchArrayValues($FUNCTION_ARRAY, $FUNCTION_KEY1, $FUNCTION_KEY2)
  {
  $FUNCTION_TEMPVALUE = $FUNCTION_ARRAY[$FUNCTION_KEY2];
  $FUNCTION_ARRAY[$FUNCTION_KEY2] = $FUNCTION_ARRAY[$FUNCTION_KEY1];
  $FUNCTION_ARRAY[$FUNCTION_KEY1] = $FUNCTION_TEMPVALUE;
  RETURN($FUNCTION_ARRAY);
  }

$myarray[0] = "hans";
$myarray[1] = "klaus";
$myarray[2] = "peter";
print_r(array_values($myarray));

$myarray = SwitchArrayValues($myarray, 0, 2);
print_r(array_values($myarray));
?>


Aber ich finde die da nirgends. Das gibt's doch nicht, oder? :gruebel: