Adding Data To An Existing PHP Array
In our last example we broke down what an array is and all the parts to make an array and of course two ways to display the information in the array. Now we are going to add more variables to array but not how you might think by just editing the script and then adding the information in, instead you will be editing the file by adding new information to the existing file.
Granted it don’t make sense but if you get into MySQL database design it will make complete sense.
Now working from the previous example
// Script 7.1 - soups1.php /* This script creates and prints out an array. */ // Address error management, if you want. // Create the array: $soups = array ( 'Monday' => 'Clam Chowder', 'Tuesday' => 'White Chicken Chili', 'Wednesday' => 'Vegetarian' ); // Try to print the array: print "$soups"; // Print the contents of the array: print_r ($soups);
We are going to add three more days of which of course come with three more soups and how this is done is instead of using the => we will be using just a = sign that will go between the key and the value like so:
$soups['Thursday'] = 'Chicken Noodle';
Again, it doesn’t make much sense to do this because of the fact you can just easily just to the existing array, but again if your going to use MySQL Database you will most likely have a form of some sorts to enter the data quickly or make it dynamic enough so other people could enter the data and then the database would update itself. You see you will set up your PHP script to automate the process however it will connect to a database and there you will display the data dynamically.
After getting side track a bit we are going to do two things to this script, first we are going to set up an array count and thus the php will count the number of items in the array, next we are going to add the new information that will connect to the array by use the $soup key as the identifier. After that we are going to echo the count and that will be it.
// Script 7.2 - soups2.php /* This script creates and prints out an array. */ // Address error management, if you want. // Create the array: $soups = array ( 'Monday' => 'Clam Chowder', 'Tuesday' => 'White Chicken Chili', 'Wednesday' => 'Vegetarian' ); // Count and print the current number of elements: $count1 = count ($soups); print "The soups array originally had $count1 elements."; // Add three items to the array: $soups['Thursday'] = 'Chicken Noodle'; $soups['Friday'] = 'Tomato'; $soups['Saturday'] = 'Cream of Broccoli'; // Count and print the number of elements again: $count2 = count ($soups); print "<p>After adding 3 more soups, the array now has $count2 elements.</p>"; // Print the contents of the array: print_r ($soups);
Now done correctly with the help of a PHP include you should see Thursday, Friday and Saturday in the array.
<body>
< ? php include("script_07_02.php"); ? >
</body>
So now you know at least two ways to add more data to the array, but hopefully once your get into MySQL programming you will learn the third way and thus make this process go by a whole lot quicker.













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