473,657 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Command to Print or Echo all Elements of an Array?

tharden3
916 Contributor
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?
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'?
Jan 3 '09 #1
3 9440
Dormilich
8,658 Recognized Expert Moderator Expert
@tharden3
does it really matter if you see both values? I'm glad for any information I get...

regards
Jan 3 '09 #2
Atli
5,058 Recognized Expert Expert
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. }
Jan 3 '09 #3
tharden3
916 Contributor
Oh, ok. That's like saying, "for x in this, do this". Thanks for the help.
Jan 3 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1716
by: opt_inf_env | last post by:
Hello, What is a common way to print elements of a multidimensional array? I treid to do it in the straightforward way: $input_list = "Something"; print "$input_list"; And it does not work. The solution that I found is to use intermediate variable: $array = $input_list;
4
2523
by: Shailesh | last post by:
Hi! I want to overload << operator so that it can print an arry defined in MyClass.My problem is that I want to print only a no of elements NOT all the elements in the array and this no of elements depends upon user input.The code below describes this in detail: class MyClass { public: unsigned short x;
2
1365
by: Dave Thomas | last post by:
I have an array, say something like this (to build a form): $commands = array( "Start Date" => "build_date('startdate')", "End Date" => "build_date('enddate')" ); Only it will have many more entries. I want to cycle through the array, and print the first part, but then execute the second part so that it goes to the function build_date (which returns html code to output).
3
3480
by: Eli Criffield | last post by:
I'm try run an ssh command in pexpect and I'm having trouble getting everything escaped to do what i want. Here's a striped down script showing what i want to do. -- #!/usr/bin/env python import pexpect import sys if len(sys.argv) < 3:
2
7518
by: mordac | last post by:
Hello, I was wondering if I could get some opinions on how best to handle printing in a max heap data structure. Right now my heap struct looks as thus: typedef struct heapStruct { int* heapArray; int heapSize; int arraySize; int maxSize; } heapStruct; arraySize holds the current number of elements in the array.
1
2318
by: Steff | last post by:
I am wandering if my code is making sense... I use a lot the print function. Is it weird in this case where I have to display an array ? I thought it would be better to have the entire array in php but now I am not sure if that makes sense. Can you tell me please ? <html> <head>
1
3378
by: Manish | last post by:
I have the following code in a script -------------------------------------------------------------------------------------------------------------------------------- foreach($serverlist as $sno=>$serverdata) { $servername = $serverdata; if($servername) { unset($outputstr, $outputarr); $outputstr = exec("ssh -n ".$servername." 'fstat | wc -l'", $outputarr);
4
4595
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the length of my queue. I started getting compilation errors when I included a length function. <code> template<class ItemType> void Queue<ItemType>::MakeEmpty() {
1
2531
by: sheel331 | last post by:
Hello, I am new to coding in PHP, and had a question about a simple thing: I am trying to echo out the elements of an array for a sidenav in one of my HTML pages, and I can't seem to figure out the syntax for appending the element of the array into the path to the file. Here is the code I have: <?php include 'c:\inetpub\webroot\sheel\template\navbar.html';
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.