Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    62
    Thanks
    1
    Thanked 0 Times in 0 Posts

    display array value only if present

    Hello, I'm stuck on an array problem. I have the following array of session vars from a user form:

    PHP Code:
    $quantity = array(=> $_SESSION['a2dfam'], $_SESSION['a2dind'], $_SESSION['a2dstu'], $_SESSION['a1dfam'], $_SESSION['a1dind'], $_SESSION['a1dstu'
    Note that not all of these values will be set (e.g., if the user didn't need to order something from each category). Therefore, on a verification page, I want to display totals only for the items that a user wants. You'll notice that the array values are in sets of 3 (family, individual, student).

    Is it possible to loop through a set of 3 specific values in this array, determine whether ANY of those 3 are set, and if so, output specific code? I can't figure out how to search a subset of an array, and execute code if any of those values are present.

    Thanks,

    Ryan

  • #2
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    62
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I can't figure out how in_array would work in this case. Because these array values came from a user form, they are all "set". However, I only want the non-blank values.

    Please let me know if there's an easier way, but I'm trying to do the following:

    PHP Code:
    function checkarr($start$end){
        
    $c=0;
        for (
    $i=$start$i<=$end$i+=1) {
            if (
    $quantity[$i]!="") {
                
    $c++;
            }
        }
        return 
    $c;
    }

    if (
    checkarr(1,3) > 0) {code to execute


  •  

    Posting Permissions

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