1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How can i improve this function

Discussion in 'PHP' started by bweb11, Aug 10, 2009.

  1. #1
    Hi, can someone please help me improve this function i wrote to display the contents of an array without using print_r


    function printarray($arr)
    {

    foreach ($arr as $key => $value)
    {
    if($value !== "" && $value !== " " && !empty($value) && isset($value))
    {
    if(gettype($value) == 'array') {echo "$key: "; $this->printarray($arr[$key]);}
    else{echo "$key => $value<br />\n";}
    }

    }

    }
     
    bweb11, Aug 10, 2009 IP
    Alice24 likes this.
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    I would use a for loop instead of foreach. I will also just trim the value and check if it is empty or not, instead of doing 2 function calls and two checks. Lastly, I might also use is_array instead of gettype.
     
    ThePHPMaster, Aug 10, 2009 IP
  3. bweb11

    bweb11 Peon

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, ThePHPMaster, but i don't think you can access the keys and values of array with for like you can with foreach.

    I've take your advice for the is_array and to trim it.

    The function now as it stand is:

    function printarray($arr)
    {

    foreach ($arr as $key => $value)
    {
    if($value !== "" && $value !== " " && !empty(trim($value)) && isset($value))
    {
    if(is_array($value)) {echo "$key: "; $this->printarray($arr[$key]);}
    else{echo "$key => $value<br />\n";}
    }

    }

    }

    Any other suggestions please?
     
    bweb11, Aug 10, 2009 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Here is my final take.

    
    function printarray($arr)
    {
       $count = count($arr);   
       $keys = array_keys($arr);
       for($x=0;$x<$count;$x++)
       {
          $key = $keys[$x];
          $value = trim($arr[$key]);
    
          if(strlen($value) > 0)
          {
             if(is_array($arr[$key])) {
                echo "$key: "; 
    			printarray($arr[$key]);
    			echo "<br />\n";
             } else{
                echo "$key => $value<br />\n";
             }
          }
       }
    } 
    
    PHP:
     
    ThePHPMaster, Aug 10, 2009 IP
  5. Guttu

    Guttu Peon

    Messages:
    728
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This is much better.
     
    Guttu, Aug 11, 2009 IP
  6. bweb11

    bweb11 Peon

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks, it's brilliant.
     
    bweb11, Aug 11, 2009 IP
  7. HypertextFever

    HypertextFever Peon

    Messages:
    158
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Why? I'm just curious...
     
    HypertextFever, Aug 12, 2009 IP
  8. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    As am I, in this instance a foreach is completely appropriate, not only will it loop thru the correct amount, but has a way of retrieving the key without having to use a separate function. In PHP5, foreach can even iterate objects and not just arrays.
     
    kblessinggr, Aug 12, 2009 IP
  9. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #9
    I stand corrected. Normally, the foreach statement is the best construct for iterating through an array. However, if we are modifying the elements in the array, foreach works on a copy of the array and would result in added overhead. Since we are not modifying the array, foreach would just work as good as the for loop.
     
    ThePHPMaster, Aug 16, 2009 IP
  10. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Any documentation you can provide that shows a significant overhead of foreach over for?
     
    kblessinggr, Aug 16, 2009 IP
  11. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Actually, foreach in PHP5 allows you to modify array values as well:

    
    foreach ($values as &$value) {
        $value += 1;
    }
    
    PHP:
     
    premiumscripts, Aug 16, 2009 IP
  12. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #12
    You will find it on Question #10 Answer - Page 23 of "The Zend PHP Certification Practice Test Book" First Edition Jan 2005.

    You can also find it on Page 58 in "Zend PHP 5 Certification Study Guide".
     
    ThePHPMaster, Aug 16, 2009 IP
  13. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Anything actually online? such as at zend.com?
     
    kblessinggr, Aug 16, 2009 IP
  14. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #14
    Not that I know of, I only know of the above.
     
    ThePHPMaster, Aug 16, 2009 IP