Arrays: PHP 7 Complete Guide Simple, Multi-dimensional, Associative, and Object Arrays Steve Prettyman Little Ocean Waves Publishing Stone Mountain, GA Little Ocean Waves Publishing 2016 Little Ocean Waves Publishing Steve Prettyman ALL RIGHTS RESERVED. No part of this textbook may be reproduced, transmitted, or used in any form (print, graphical, electronic, storage) except as permitted under the United States Copyright Act, without prior written permission of the publisher/author. Contact for written permission. All trademarks and copyrights are the property of their respective owners. Notice of Liability Every precaution has been taken in the preparation of this book and the related web site to avoid errors or problems. However, this book and the related web site is distributed on an As is basis, without any warranty.
Neither the author (Steve Prettyman) nor the publishing companies shall have any liability to any person or entity with respect to any damage or loss caused or alleged to be caused directly or indirectly by the instructions and coding contained in this book and its related web site. The author has created fictitious names, situations, company names, and other similar items for providing understanding of the concepts to the reader. Any resemblance of these fictitious items to any person, company, organization, place, event, or location is unintentional and purely coincidental. Preface Arrays - PHP 7 Complete Guide is intended for use as a supplemental beginning level programming book. It is not the goal of this book to cover advanced techniques in the current versions of the PHP programming language. Some beginning knowledge of general PHP programming concepts is expected but no actual programming experience or education is assumed.
All code examples in this book are compatible with PHP 7. The newest methods (functions) available in PHP have been used to provide the reader with the most current coding techniques. The examples use core methods provided in the PHP language. PHP includes many additional methods to accomplish similar tasks as shown within. The reader may, and should, research additional advanced array techniques after understanding the material presented in this book. Special Note Teachers This book is provided as a supplementary guide to introductory textbooks on PHP 7.
The intent of this book is to provide additional examples and explanation of the power and use of arrays in the PHP language. PHP arrays provide many capabilities that arrays in other languages do not provide. Teaching tools, including test banks, course outline, and PowerPoint slides are available for use from the textbook web site. Since this book is created using Amazons CreateSpace, this book can be redesigned to meet your classroom needs. Feel free to contact the author (Steve Prettyman, ) for more information. A free PDF version of this book is available to verified instructors upon request.
Please e-mail the author (Steve Prettyman, ) using the official school e-mail. Please also include a link to the schools contact page to verify teaching status. Code Examples, Images, and Links Every effort has been made to catch any errors in code (and grammar). Please let us know if/when you discover problems in this book. By using Amazons CreateSpace on demand publishing, errors can be quickly corrected and updated before additional copies of the book are printed. Please send all corrections to Steve Prettyman ( ).
All code examples, images, and links are available for download from the location below. Please download code examples from the web site. Copying code from the book may cause errors due to format requirements for publishing. Textbook Web Site www.littleoceanwaves.com/understandingarrays/ About the Author Steve Prettyman earned his Bachelors of Arts Degree in education from Oglethorpe University in 1979. He quickly began his teaching career as a high school mathematics instructor while continuing his education by earning a Masters Degree in Business Information Systems from Georgia State University (1985). Since then, Steve has spent over 30 years in the IT industry.
The last, almost 20 years, he has been an instructor and professor at Chattahoochee Technical College, Kennesaw State University, and Southern Polytechnic State University. His primary teaching responsibilities include programming, web design, and web application development. Dedication This book is dedicated to my wife Beverly. Thank you for over 20 years of love and support, without you this book would not be possible. Table of Contents Chapter 1 Simple Arrays What are arrays? Why do we need to use them? Other Ways to Define Arrays Html Arrays Deleting Updating & Inserting Exercises Chapter 2 Multi-dimensional Arrays Html Arrays Deleting Updating & Inserting Exercises Chapter 3 Associative and Object Arrays Deleting Updating & Inserting Object Arrays Exercises Chapter 4 PHP Functions Changing, Spliting, Slicing, and Sorting Arrays Changing Array Contents Spliting and Slicing Arrays Sorting Arrays Exercises Chapter 5 PHP Functions Comparing and Merging Arrays Comparing Arrays Merging Arrays Exercises Chapter 6 PHP Functions Searching, Traversing, and Displaying Arrays Searching Arrays Traversing Arrays Displaying Array Contents Exercises
Chapter 1 Simple Arrays
After completing this chapter, the student will be able to Define and describe the advantages in using arrays Create an html form that validates information contain an html array Create a simple PHP array Save values into a simple PHP array Display values in a simple array Add values from an html form into a simple array Validate values before placing them into an array
What are arrays? Why do we need to use them?
Whenever a program uses information, that information must be stored in the memory of the computer. $propertyname = value; Property names are declared using the $ and a descriptive name for the property. $propertyname = value; Property names are declared using the $ and a descriptive name for the property.
PHP property names begin with a lowercase letter. If the programmer wants to use more than one word for the name, the camel case format and/or separation of the words via a special character (usually the _) can be used. $firstName = ; $first_Name = ; $first_name = ; Property names should be meaningful to make your program more readable. $a = ; $last_name = ; In the example above, $a is not meaningful. We do not have any indication of what might be stored. However, $last_name is meaningful, we understand that a persons last name will be stored.
PHP itself will not stop you from declaring a property with a capital letter. However, that is usually reserved for class names and constants. Lets take a quick look at a coding example that stores information using a property (or variable). Our example program will request personal information (such as name, address, city, state, zip) from a user via a web form. Image 1-1 example1.html Example 1-1 example1.html
Please enter information in all fields
First Name
Last Name
Address
City
State
Zip code
Note: Some indenting and whitespace has been removed from the examples in this book for better viewing in your Kindle device. Download the code from the authors website to assist you in understanding the code.
Example 1-1 provides a pretty typical web form that requests information from the user. The html shown also filters the information accepted by the user, using html 5, to insure that information was provided in the proper format. Note: If you dont know html, you should review some of the free tutorials and videos provided on the web. PHP is a web application language which commonly interfaces with html and JavaScript. Once the user enters the information in the proper format and hits the submit button, the information will be sent to a program on the web server for processing. In this example, the html form line indicates that the process_customer.php application will accept and handle the information.
Next page