More on PHP Arrays & How To Display Array Data
In my earlier examples we had briefly talk about array’s, well this week it is all about the php array. So what is an array? A array is a collection of singular values(meaning one value) that are put together and assembled to make one variable based on all the information in that array.
Example of an Array
$soups = array ( 'Monday' => 'Clam Chowder', 'Tuesday' => 'White Chicken Chili', 'Wednesday' => 'Vegetarian' );
The most common way to use an array is to use the print command to display the data in that array like so:
print "$soups";
Then, when you trigger the php script, it will display the information in the array like so
Clam Chowder
White Chicken Chili
Vegetarian
Lets break down what is included in a array shall we
$soups = is the name of the array and usually to begin that process you start with the dollar sign ($) and then add a name to it.
Next in order to begin the array you need to type in the actual word array and then begin it with ( and then end it with a ); and in between the ( ); is were the bindable data will be stored and then later displayed once trigger.
So far we have this $soups = array ( );
Now it is time to put the actual data in which your provide two variables the first one a key which can be an integer or string in this case Monday would be out key since we are doing a soup of the day type script. The next part is the value which can be anything your want and in this case it is Clam Chowder. As for the => symbol I would guess that it is just a way to connect the key and value together by pointing the key to the value, don’t know if it has a name or not.
Then once we put it together it will looking something like this ‘Monday’ => ‘Clam Chowder’, now if you remember my discussion on single quotes and double quotes, you would want to use single quaotes all the time when setting up an array.
But what if you want to add more data into the array? simple enough just drop a comma after the first bit of information and go on from there, but make sure the last value ends with no comma or you will produce an error, most likely a blank page
Example of a Multiple Array
‘Monday’ => ‘Clam Chowder’, ‘Tuesday’ => ‘White Chicken Chili’, ‘Wednesday’ => ‘Vegetarian’
I would like to point out that there are two ways to list an array
The first one is done like so
$soups = array ( 'Monday' => 'Clam Chowder', 'Tuesday' => 'White Chicken Chili', 'Wednesday' => 'Vegetarian' );
The second one, you can do in a straight line like in the example I have in the multiple array, but the one difference is that you wouldn’t have the => there as the all the values would be used within that key.
$week = array ( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' );
Those kind of arrays you would see most commonly in forms that are used to collect and store data.
Now that we know a little more about arrays, lets give example of an array in action by using the code from the examples given.
External PHP Script
// Create the array: $soups = array ( 'Monday' => 'Clam Chowder', 'Tuesday' => 'White Chicken Chili', 'Wednesday' => 'Vegetarian' ); // Try to print the array: print "<p>$soups</p>"; // Print the contents of the array: print_r ($soups);
Now with the help of a PHP include I will be displaying the soup of the day for you
<body>
< ? php include("script_07_01_1.php"); ? > // remove space from php tags
</body>
You will notice that the information in the array never got printed like it should have instead we had to use the print_r function in order to display the data and the reason why the data is different is due to the different structure type an array variable has.
Although php includes will help trim down on making documents larger, just remember though that you will have to set the scripts up to match the CSS so not break the layout. So that’s about it for this example and so I would recommend that you search out and see what other ways you can set up and display array data.













Leave your response!
You must be logged in to post a comment.