Results 1 to 6 of 6

Thread: replace empty array element

  1. #1
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default replace empty array element

    i have a form with a text field that is an array

    Code:
    <input name="quantity[]" type="text" id="quantity[]" size="4" 
    maxlength="4" class="smallText" value="<? inputval2($_POST['quantity'], 
    $row_rsCatDetails['quantity_cmp'])?>"/>
    when the form is submitted i want to replace the elements in the array that does not have a value with a "0".
    tried this but it does not work:
    Code:
    $qty = $_POST['quantity'];
    
    $needle = "";
    $replace="0";
    for($i=0;$i<count($qty);$i++){
    if($qty[$key] == $needle){
    $qty[$key]=$replace;
    }
    }
    Last edited by leocrawf; Apr 2, 2009 at 06:39 PM.
    "...men are that they might have joy."
    Nephi

  2. #2
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default

    not sure what you doing but do a print_r($_POST['quantity']); and verify that the array is as you think it is.

    it is often best to use a foreach($arr as $key=>$value){ } is stead of a for loop.

  3. #3
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default

    Quote Originally Posted by owen View Post
    not sure what you doing but do a print_r($_POST['quantity']); and verify that the array is as you think it is.

    it is often best to use a foreach($arr as $key=>$value){ } is stead of a for loop.
    I do have a valid array and i can see the values by doing this:
    Code:
    echo $show = implode(",",$qty);
    example result: '','','','7','10','23','34', etc
    however there is the problem when there is no value in the beginning elements , eg: '','','', etc, when i am ready to update and do my for loop the values are not passed to the right rows id because there was no value in them. So what i want to do is put a zero to replace the '','','', as a space holder if you will, so that the right values will be passed to the specific rows in the database.

    Maybe there is a better way to do it.
    "...men are that they might have joy."
    Nephi

  4. #4
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default

    Well after much thought i decided to scrap that method and use this instead, now i want to remove all empty elements and this is the solution that worked for me.


    Code:
    foreach($qty as $key => $value) {//loop through all the values in the array
       if ($value == NULL || $value == "" ) {//check if any elements is empty
          unset($qty[$key]);//delete empty element
       }
    }
    $qty = array_values(qty); //create a new array
    
    print_r($qty);//show me
    Last edited by leocrawf; Apr 6, 2009 at 02:37 PM.
    "...men are that they might have joy."
    Nephi

  5. #5
    Join Date
    Sep 2008
    Posts
    59
    Rep Power
    0

    Default

    Why not sanitize the data first eg check if the entry is a 0 or empty.

    get all values from the form and do the check , then rebuild the array

    form > array > check if empty > rebuild the new array

    job done

    no need to do a unset , if you want you can do that also if the next part needs a value

  6. #6
    Join Date
    Jul 2004
    Posts
    153
    Rep Power
    0

    Default

    Just add to clarity, your original solution could have worked had you written this

    Code:
    if($qty[$i] == $needle){...}
    instead of

    Code:
    if($qty[$key] == $needle){...}
    Notice that $key was not defined anywhere in the code snippet you provided.

Posting Permissions

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