Connecting Tech Pros Worldwide Help | Site Map

Command to Print or Echo all Elements of an Array?

  #1  
Old January 3rd, 2009, 07:12 AM
tharden3's Avatar
Site Addict
 
Join Date: Jul 2008
Location: Ocala, FL (United States)
Posts: 819
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($months) or something?
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $months=array('January','February','March','April');
  3. $key=0;
  4. $calendar=count($months);
  5. while($key<=$calendar){
  6. echo "$months[$key]<br/>";
  7. $key++;}
  8. ?>
  9.  
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; January 3rd, 2009 at 07:17 AM. Reason: Found new possible answer.
  #2  
Old January 3rd, 2009, 09:56 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,487
Provided Answers: 9

re: Command to Print or Echo all Elements of an Array?


Quote:
Originally Posted by tharden3 View Post
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
  #3  
Old January 3rd, 2009, 03:18 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: Command to Print or Echo all Elements of an Array?


Hi.

You can use print_r to see your array in a formatted way.
I usually just do:
Expand|Select|Wrap|Line Numbers
  1. echo "<pre>", print_r($array, true), "</pre>";
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
Expand|Select|Wrap|Line Numbers
  1. foreach($months as $month) {
  2.   echo "$month<br />";
  3. }
  #4  
Old January 3rd, 2009, 05:14 PM
tharden3's Avatar
Site Addict
 
Join Date: Jul 2008
Location: Ocala, FL (United States)
Posts: 819

re: Command to Print or Echo all Elements of an Array?


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 5 November 14th, 2005 12:36 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM