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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts

    Looping through array and seeing which values are empty

    I have an array like this

    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    id] => 139
                
    [name] => Eithne Maun
                
    [contactNo] => 877692398
                
    [email] => eithne@yahoo.co.uk
                
    [address] => 
                [
    county] => Cork
                
    [country] => Ireland
                
    [sourceOfContact] => Phone
                
    [type] => Corporate
                
    [dateAdded] => 2012-11-26
                
    [companyName] => 
                [
    dateOfBirth] => 
            )


    I want to loop through each element in the array and only display the lines below if the value is not blank
    PHP Code:
    Number: <?php htmlout($contact[0]['contactNo']);?><br />
                    Email: <?php htmlout($contact[0]['email']);?><br />
                    Address: <?php htmlout($contact[0]['address']);?><br />
                    County: <?php htmlout($contact[0]['county']);?><br />
                    Country: <?php htmlout($contact[0]['country']);?><br />
                    Type of Contact: <?php htmlout($contact[0]['type']);?><br />
                    Company Name: <?php htmlout($contact[0]['companyName']);?><br />
                    Date of Birth: <?php htmlout($contact[0]['dateOfBirth']);?><br />
    Is there a function with a loop that i could use??

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    use foreach with keys

    PHP Code:
    foreach($contact[0] as $key => $val) {
      echo 
    "{$key} => {$val}\n";



  •  

    Posting Permissions

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