Deleting From Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
massiveone
Forum Commoner
Posts: 29
Joined: Tue Jun 18, 2002 4:39 pm
Location: Canada
Contact:

Deleting From Arrays

Post by massiveone »

Here is my problem if anyone can help :lol: , its more of a structual issue
heres what i got. I am trying to think how to use the unset command to destroy a line in my array. but write the code so that it works with the button... any ideas anyone? thanks in advance.

print "<table>";
foreach ($skus as $sk => $qt ) {
print "<tr><td bgcolor=$tog>$sk</td>";
print "<td align='right' bgcolor=$tog>$qt</td>";
print "<td><input type=button value=Del </td><tr>";
}
print "</table>";
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

User avatar
massiveone
Forum Commoner
Posts: 29
Joined: Tue Jun 18, 2002 4:39 pm
Location: Canada
Contact:

Post by massiveone »

the slice function is very cool thanks for the links, unset would probably work better as its less math and more direct like unset(key,value) but i dont know how to put it in a form button...
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Re: Deleting From Arrays

Post by xisle »

print "<table>";
foreach ($skus as $sk => $qt ) {
print "<tr><td bgcolor=$tog>$sk</td>";
print "<td align='right' bgcolor=$tog>$qt</td>";
print "<td><input type=button value=Del </td><tr>";
}
print "</table>";

... think unset would wipe the entire array. Others will respond if I am wrong .. :D


I would do something like this assuming you want a seperate button for each element, the array does not have to be passed every time, and register globals is active:

Code: Select all

if($del != "")&#123;
  $skus&#1111;$sk]="";
&#125;

print "<table>";
foreach ($skus as $sk => $qt ) &#123;
	if($qt !="")&#123;
	   print "<tr><td bgcolor=$tog>$sk</td>";
	   print "<td align='right' bgcolor=$tog>$qt</td>";
	   print"<form method="POST" action="$PHP_SELF">";
	   print"<input type="hidden" name="sk" value="$sk">";
	   print "<td><input type="button" name="del" value="Del">
          </td><tr>";
	   print "</form>";
	&#125;
&#125;
print "</table>";

xisle
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

If you want to retain a coherent index ([0],[1],[2],[3] etc.) you should use array_splice.
If you don't, just unset($array[$key]), example:

Code: Select all

<?php
if(isset($_POST["del"]) && !empty($_POST["del"]))
   unset($skus[$_POST["del"]]);
?>
User avatar
massiveone
Forum Commoner
Posts: 29
Joined: Tue Jun 18, 2002 4:39 pm
Location: Canada
Contact:

Post by massiveone »

Cool Thanks All for your help. Here was my final solution., when i click on the del button it now removes the item from the array... the array is associative with format SKU => Quantity ... so you can read this easier.

Code: Select all

echo "<form name=fin>";
 if ($skus) &#123;
    print "<center>Items Added so far</center>";
    print "<center><table cellspacing=1 bgcolor='4682B4' ><tr><td><b>SKU</b></td><td align='right'>Quantity</td></tr>";	
	
	$tog="E0FFFF";
	foreach ($skus as $sk => $qt ) &#123;
 	
	print "<tr><td bgcolor=$tog>$sk</td>";
	print "<td align='right' bgcolor=$tog>$qt</td>";
	//print "<td><input type=name=delete&#1111;%s] value='Del'> </td></tr>";
	printf(" <td> <input type='submit' name=delete&#1111;%s] value='Del'> </td>\n",$sk);
	
	&#125;
    
	print "</table></center>";
	echo "<input type=hidden name=option value=addcos>";
    echo "<input type=hidden name=companyid value=$companyid>";
 &#125;
 echo "</form>";
 
 if (is_array($delete)) &#123;
  foreach ($delete as $this => $junk) &#123;
    unset ($skus&#1111;$this]);
  &#125;
 &#125;
 
 &#125;
Post Reply