unset
Michael W.
- php
0 MADU0 Joachim0 horst0 Michael W.0 horst0 Michael W.
0 Joachim0 Michael W.0 Joachim
Hallo,
ich habe hier ein script das mir mein array löscht:
<?php
$arr = array();
$a = 'a';
$b = 'b';
$c = 'c';
$arr[] = $a;
$arr[] = $b;
$arr[] = $c;
print "erstmal gucke:<br>";
echo join (',',$arr);
print "<br><br>";
unset ($arr);
if (isset ($arr))
{
Print "Hier:<br>";
echo join (',',$arr);
}
else
{
print "das array wurde komplett gelöscht";
}
?>
jedoch möchte ich an der stelle des "unset"s gerne eine funktion die mir a, b oder c aus dem array KOMPLETT entfernt und keine lücken hnterlässt, so dass die ausgabe am ende so aussieht:
wenn ich a lösche:
b,c
wenn ich b lösche:
a,c
wenn ich c lösche:
a,b
einfach oder ? nur finde ich keine funktion die mir das erlaubt.
Bin für jede hilfe dankbar.
Hallo,
jedoch möchte ich an der stelle des "unset"s gerne eine funktion die mir a, b oder c aus dem array KOMPLETT entfernt und keine lücken hnterlässt [...]
http://www.php.net/manual/en/ref.array.php
auf dieser seite gibt's bei den user notes einen eintrag vom 13. juni 2002, der eine funktion "deleteandpush" enthält.
lg
MADU
hi,
wirf doch da mal einen Blick drauf:
http://www.php.net/manual/de/function.array-splice.php
hth + Gruesse Joachim
lass dir nicht immer alles vorkauen...
lies auch mal was:
http://www.php.net/manual/de/function.array-splice.php
gruss
horst
lass dir nicht immer alles vorkauen...
lies auch mal was:
http://www.php.net/manual/de/function.array-splice.php
ooh mann - da war ich vieleicht schon !!
diese funktion macht doch genau das wo ich nicht weiterkomm
die werte löschen und nicht entfernen.
http://mitglied.lycos.de/djwoelk3/test/zeile/start.php
!!
$input = array ("rot", "grün", "blau", "gelb");
/*
0 rot
1 grün
2 blau
3 gelb
*/
array_splice ($input, 2,1);
foreach($input as $key => $value){
echo "$key $value<br>";
}
gibt bei mir ganz brav:
0 rot
1 grün
2 gelb
aus...
irgendwas anderes muss also falsch laufen
gruss
horst
hier ist das script zu dem link:
<?php
session_start();
$one = '1';
if (isset ($arr_nr))
{
$nr = $_GET['nr'];
$euro = $_GET['euro'];
$text = $_GET['text'];
$arr_nr[] = $nr;
$arr_euro[] = $euro;
$arr_text[] = $text;
$summe = array_sum($arr_euro);
print "<table border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0" width="70%">";
print "<tr>";
print "<td align="center" width="41">Nr.</td>";
print "<td align="center" width="100">Bestell-Nr.</td>";
print "<td align="center" width="270">Beschreibung</td>";
print "<td align="center">Preis</td>";
print "</tr>";
for($i=1;$i<count($arr_nr);$i++)
{
$ii = $i-$one;
print "<tr>";
print "<td align="center">".$i."<a href="karte.php?del=$ii">Löschen</a></td>";
print "<td align="center">".$arr_nr[$i]."</td>";
print "<td align="center">".$arr_text[$i]."</td>";
print "<td align="center">".$arr_euro[$i]." € </td>";
print "</tr>";
}
print "<tr>";
print "<td align="center"></td>";
print "<td align="center"></td>";
print "<td align="center"></td>";
print "<td align="center"></td>";
print "</tr>";
print "</table>";
print "<table width="70%">";
print "<tr>";
print "<td align="center" width="450"></td>";
print "<td align="center" style="border-style: solid; border-width: 1">".array_sum($arr_euro)." € </td>";
print "</tr>";
print "</table>";
session_register("arr_nr");
session_register("arr_euro");
session_register("arr_text");
}
else
{
print "Ein Fehler ist aufgetreten starten sie die Seite Neu. <a href="start.php">Neu Starten ...</a>";
}
?>
<br>
<br>
<br>
<a href="karte.php?nr=001&euro=4.50&text=Salami Pizza Classic">001</a>
<a href="karte.php?nr=002&euro=5.00&text=Hawaii Pizza">002</a>
<a href="karte.php?nr=003&euro=1.50&text=Cola">003</a>
<br>
<br>
<br>
<a href="start.php">Neu Starten ...</a>
es leigt vileicht dadran das ich kein foreach im script habe der mir es als einzelne variable ausgibt, oder ?
jetzt habe ich es nochmal neu probiert:
<?php
session_start();
$one = '1';
if (isset ($arr_nr))
{
$nr = $_GET['nr'];
$euro = $_GET['euro'];
$text = $_GET['text'];
$arr_nr[] = $nr;
$arr_euro[] = $euro;
$arr_text[] = $text;
$offset = $_GET['del'];
if (isset ($del))
{
array_splice ($arr_nr, $offset, 1);
array_splice ($arr_euro, $offset, 1);
array_splice ($arr_text, $offset, 1);
}
$summe = array_sum($arr_euro);
print "<table border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0" width="70%">";
print "<tr>";
print "<td align="center" width="41">Nr.</td>";
print "<td align="center" width="100">Bestell-Nr.</td>";
print "<td align="center" width="270">Beschreibung</td>";
print "<td align="center">Preis</td>";
print "</tr>";
for($i=1;$i<count($arr_nr);$i++)
{
print "<tr>";
print "<td align="center">".$i."<a href="karte.php?del=$i">Löschen</a></td>";
print "<td align="center">".$arr_nr[$i]."</td>";
print "<td align="center">".$arr_text[$i]."</td>";
print "<td align="center">".$arr_euro[$i]." € </td>";
print "</tr>";
}
print "<tr>";
print "<td align="center"></td>";
print "<td align="center"></td>";
print "<td align="center"></td>";
print "<td align="center"></td>";
print "</tr>";
print "</table>";
print "<table width="70%">";
print "<tr>";
print "<td align="center" width="450"></td>";
print "<td align="center" style="border-style: solid; border-width: 1">".array_sum($arr_euro)." € </td>";
print "</tr>";
print "</table>";
session_register("arr_nr");
session_register("arr_euro");
session_register("arr_text");
}
else
{
print "Ein Fehler ist aufgetreten starten sie die Seite Neu. <a href="start.php">Neu Starten ...</a>";
}
?>
<br>
<br>
<br>
<a href="karte.php?nr=001&euro=4.50&text=Salami Pizza Classic">001</a>
<a href="karte.php?nr=002&euro=5.00&text=Hawaii Pizza">002</a>
<a href="karte.php?nr=003&euro=1.50&text=Cola">003</a>
<br>
<br>
<br>
<a href="start.php">Neu Starten ...</a>
und dann passiert folgendes beim löschen:
es wird gelöscht was gelöscht werden soll aber es wird nach oben hin aufgerückt (das ist soweit genau so wie ich es wollte), nur bleibt die letzte zeiel LEER die soll auch weg.
http://www.php.net/manual/de/function.array-pop.php
ist mein freund.
das wars der onlineshop ist fertig.
http://mitglied.lycos.de/djwoelk3/test/zeile/start.php
hi,
diese funktion macht doch genau das wo ich nicht weiterkomm
die werte löschen und nicht entfernen.
Natuerlich werden die Arrayelemente entfernt, teste mal das Beispiel:
Gruesse Joachim
<html>
<head>
</head>
<body>
<br>
<?php
$xxx = array ("lala","blah","buh","urgs","arrg","jep");
// Testausgabe
echo "Original : " . implode (", ", $xxx) . "<br>";
echo "Anzahl Elemente: " . count ($xxx) . "<br><br>";
// entferne ab Pos 1 ein Element
$yyy = array_splice ($xxx, 1, 1);
// Testausgabe
echo "array_splice(arr, 1, 1) : " . implode (", ", $xxx) . "<br>";
echo "Wech sind :" . implode (", ", $yyy) . "<br><br>";
// entferne ab Pos 1 zwei Elemente
$yyy = array_splice ($xxx, 1, 2);
// Testausgabe
echo "array_splice(arr, 1, 2) : " . implode (", ", $xxx) . "<br>";
echo "Wech sind : " . implode (", ", $yyy) . "<br>";
echo "Anzahl Elemente: " . count ($xxx);
?>
</body>
</html>