www.webdeveloper.com
Results 1 to 5 of 5

Thread: Simple Array Search

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Posts
    97

    Simple Array Search

    Newbie here. What am I missing? I'm trying to see if the array contains a 0 to throw the error.

    Code:
    $s=$_POST['Qty'];
    		$flag=1;
    		$myarray = array($flag);
    		foreach (range(1, $s) as $number) {
    			$flag=1;
    			if (empty($_POST['Serial_'.$number])) $flag=0;
    		}
    		if (in_array(0, $myarray, true)) $crash[] = array("Missing: Serial Number(s){$info}","Please complete the missing information below.");
    Thanks in advance.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,144
    I think maybe you meant to do this?
    Code:
    if (empty($_POST['Serial_'.$number])) $flag[]=0;
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Feb 2005
    Posts
    97
    Thanks. I want to build $myarray from the value of the $flag, right? I'm sure there's a more efficient way of writing this code. I appreciate your help!

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,144
    Sorry, not sure what I thought I was seeing.

    Maybe something like....
    PHP Code:
    $s=$_POST['Qty'];
            
    $flag=1;
            
    $myarray = array($flag);
            foreach (
    range(1$s) as $number) {
                
    $flag=1;
                if (empty(
    $_POST['Serial_'.$number])) $flag=0;
                
    $myarray[] = $flag//  <---- does this do it?
            
    }
            if (
    in_array(0$myarraytrue)) $crash[] = array("Missing: Serial Number(s){$info}","Please complete the missing information below."); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Feb 2005
    Posts
    97
    Thanks! I'll give it a try.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles