Command to Print or Echo all Elements of an Array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tharden3
    Contributor
    • Jul 2008
    • 916

    Command to Print or Echo all Elements of an Array?

    Is there a quick way to ask to 'echo' or 'print' all elements of a numeric array? Can I specify to print "all of the keys" or "all of the values"? I wrote up 5 quick lines of code to print all of my values, but there has to be a built_in function I can call to do that for me right? Like function($month s) or something?
    Code:
    <?php
    $months=array('January','February','March','April');
    $key=0;
    $calendar=count($months);
    while($key<=$calendar){
    echo "$months[$key]<br/>";
    $key++;}
    ?>
    Do I use var_dump()? This would allow me to see the entire thing, but what if I only want to specify 'just keys' or 'just values'?
    Last edited by tharden3; Jan 3 '09, 07:17 AM. Reason: Found new possible answer.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by tharden3
    Do I use var_dump()? This would allow me to see the entire thing, but what if I only want to specify 'just keys' or 'just values'?
    does it really matter if you see both values? I'm glad for any information I get...

    regards

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      You can use print_r to see your array in a formatted way.
      I usually just do:
      [code=php]echo "<pre>", print_r($array, true), "</pre>";[/code]
      when I need to debug an array in my web pages in a hurry.

      If you need to filter or change the array before you show it, one of the Array Functions can most likely do that for you before you print it.

      P.S.
      Check out the foreach loop
      [code=php]
      foreach($months as $month) {
      echo "$month<br />";
      }[/code]

      Comment

      • tharden3
        Contributor
        • Jul 2008
        • 916

        #4
        Oh, ok. That's like saying, "for x in this, do this". Thanks for the help.

        Comment

        Working...