Results 1 to 6 of 6
  1. #1

    Adding/removing data from an array?

    I have seen/used this with other scripts, but never really understood how it worked. Could anyone provide an example where you can submit data into an array, and update it based on the selections made? For example, like a hidden form field could have the values "1,2,3,4", and explode these values to display them on the page:


    However there can be a checkbox so if you want to remove the third one, it would update the value with "1,2,4". How is this done? Thanks.

  2. #2
    I hope you guys understand my question? Basically using a form to add and remove data from an array.

  3. #3
    Join Date
    Aug 2002
    If this is in PHP, there are a variety of built-in functions you can use.

  4. #4
    Oh sorry I forgot to mention that yes it is php. Well I know the site, however I do not know what this "method" is called. Could anyone provide me with a working example please?

  5. #5
    Join Date
    Jun 2009
    Try this code:array.php
    PHP Code:
    if (!empty($_POST)) {
        if (!empty(
    $_POST['array1'])) {
    $array explode(","$_POST['array1']);
        } else {
    $array = array();
        if (
    $_POST['action'] == 'Add') {
    $elt $_POST['txtval'];
        if (
    $_POST['action'] == 'Delete')  {
            foreach (
    $_POST['check'] as $key) {


    <form name="submit" action="array.php" method="post">
    Add the value to array
    <br />
    <?if(count($array) > 0) { ?>
        <input type="hidden" name="array1" value="<? echo implode(",",$array); ?>">
     <? ?>
    <input type="text" name="txtval" size="3">
    <input type="submit" name="action" value="Add">
    <br />
    The values in array are
    <br />
    count($array) > 0) {
    foreach (
    $array as $val) {
    <input type="checkbox" value="<?=$i?>" name="check[]"><?=$val?>
    <? $i
    <br />
    <input type="submit" name="action" value="Delete">
    Hope this will help you

  6. #6
    Sweet thanks it works! As you may have figured I am using this as a shopping cart. So I can add those values into a session so the user can return to their cart and still have these values? In fact if it is not hard could you provide an example using a session? Thanks!

Similar Threads

  1. Reading data from a PHP array
    By astraeuz in forum Programming Discussion
    Replies: 6
    Last Post: 06-23-2007, 03:21 PM
  2. Adding Indices to a full pre-initialized Array C#
    By tjc1027 in forum Programming Discussion
    Replies: 3
    Last Post: 05-09-2005, 09:04 AM
  3. Store data in array after submit ..please help
    By vikaspa in forum Programming Discussion
    Replies: 0
    Last Post: 03-27-2004, 11:12 AM
  4. PHP question (open file, insert form data in array)
    By golfer in forum Programming Discussion
    Replies: 5
    Last Post: 01-01-2004, 11:44 PM
  5. How can I put this data to an array ?
    By Pere in forum Programming Discussion
    Replies: 4
    Last Post: 04-09-2003, 06:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts