SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: foreach

  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach

    Hi,

    When using:

    PHP Code:
    foreach($_POST['total'] as $total) { 
    how can you grab only records where $_POST['total'] contain data?

    Thanks

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,863
    Mentioned
    5 Post(s)
    Tagged
    2 Thread(s)
    You could try:

    Code:
    if (!empty($_POST['total'])
    {
        foreach($_POST['total'] as $total)
        ...
    }
    
    //  Or:  
    
    if (is_array($_POST['total'])
    {
        // do foreach loop
    }
    foreach will error if you attempt to use it on a non-array. If it isn't an array, obviously, you shouldn't use foreach.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by _matrix_ View Post
    Hi,

    When using:

    PHP Code:
    foreach($_POST['total'] as $total) { 
    how can you grab only records where $_POST['total'] contain data?
    Are you looking to only get the elements of $_POST['total'] that have data in them? Or only loop through $_POST['total'] if it has data in it?

    If you only want to use elements in $_POST['total'] that have values, you've got a couple options. You can't selectively choose which elements to iterate with a foreach, so you'll need to either add some logic to the loop or filter the array beforehand.

    The simplest thing would be to include an if statement in the loop.

    PHP Code:
    foreach ($_POST['total'] as $total) {
      if (
    $total) {
        
    //  $total has a value, continue processing it
      
    }

    Or, you could use the array_filter function. It allows you to pass all of the array values through a callback function and then throws them away if they fail the test. Your callback function could simply check to make sure that the element has a filled value.

    PHP Code:
    function notEmpty($var) {
      return !(empty(
    $var));
    }

    $filteredTotals array_filter($_POST['total'], "notEmpty");
    foreach (
    $filteredTotals as $total) {
      
    //  Process $totals here

    Good luck,
    - Walkere

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Array_filter automatically filters out empty values if you don't put a second argument, so the above code could be simplified to:

    PHP Code:
    foreach(array_filter($_POST['total']) as $total){
       
    //  Process $totals here

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Array_filter automatically filters out empty values if you don't put a second argument, so the above code could be simplified to:

    PHP Code:
    foreach(array_filter($_POST['total']) as $total){
       
    //  Process $totals here

    Ahh, good tip. Never actually used array_filter (except to test that snippet). Guess I should have read the doc page a little closer...

    - Walkere

  6. #6
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for all the input!

    arkinstall solution works great.

    Thanks


Bookmarks

Posting Permissions

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