PHP Tutorials – Arrays Part 2
October 20, 2008 by admin
Filed under PHP, Recent Tutorials
High Quality PHP Tutorial Videos – Taught by Experts
We also have extensive PHP Tutorials in high quality video format. These are ideal for beginners who need to master PHP quickly
Title / Free Demo : Beginners PHP Tutorial Videos
Author: Mike Morton
Duration: 6 Hours – Lessons: 86
Advanced PHP Arrays
This tutorial covers creating Arrays – Suitable for all versions of PHP
Don’t let the word advanced worry you. You are simply advancing along in your knowledge about how to use these amazing data structures.
Arrays are like tables in that they have columns and rows. And, thanks to the array functions PHP makes available, they can be manipulated like tables in many ways.
In the introduction to PHP Arrays tutorial , you learned about how to create indexed and associative arrays. I wrote that arrays were powerful tools, especially when combined with the foreach function and with the other functions designed especially for manipulating the data within arrays.
In this lesson, I’ll show you some practical uses for arrays and how to get them working their hardest for you.
Displaying All Elements in an Array
In the previous tutorial we created these two arrays:
$mystaff = array(0=>’Matthew’,
1=>’Mark’,
2=>’Luke’);
$salaries=array(“Matthew”=>65000,
“Mark”=>75000,
“Luke”=>100000);
Let’s create an array that lets us store the staff member’s name and salary together. Then we’ll use PHP’s foreach function to display them.
$mystaff[‘Matthew’]=65000;
$mystaff[‘Mark’]=75000;
$mystaff[‘Luke’]=100000;
This array structure may look odd, but all we have done is to create an associative array and use the name of each staff member as the key.
This is a great little trick that will play right into the hands of the foreach loop below. We’ll look at the syntax first and then I’ll explain the logic.
Foreach Syntax
foreach( $mystaff as $Name => $salary){
echo “Name: $Name, Salary: $ $salary “;
}
Name: Matthew, Salary: $65000
Name: Mark, Salary: $75000
Name: Luke, Salary: $10000
How It Works
The foreach statement above translates to this:
For each element of the $mystaff associative array I want to call the key by the name of $Name and the value that’s related to that key by the name $salary.
Then I want to print each of those values, one set at a time, until I reach the end of the array.
The foreach statement I provided uses the as modifier, and the as modifier uses the “=>” operator. Just think of this as a finger pointing from the key to the value.
Create a new PHP document and name it foreach.php. Copy the code below into the document and upload it to your web server and open the page in a browser.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//
EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Arrays</title>
</head>
<body>
<?php $mystaff[‘Matthew’]=65000; $mystaff[‘Mark’]=75000;
$mystaff[‘Luke’]=100000; foreach( $mystaff as $Name => $salary){
echo "Name: $Name, Salary: $ $salary "; }
echo 'I have '. count($mystaff). ' Staff members.'; ?>
</body>
</html>
PHP Array Functions
Here are examples of how PHP array-specific functions provide quick ways to enhance the power of arrays in your PHP programs.
Counting Array Elements
Count() returns the number of elements in an array. Use the Count() function to see how many staff members there are.
echo ‘I have ’. count($mystaff). ’ Staff members.’;
Sorting Array Elements
To sort the names alphabetically before displaying them, use the asort() function.
asort($mystaff);
Array Element Math
To calculate the total payroll for your staff, use the array_sum() function.
Echo ‘The total salary paid to my ‘ .count($mystaff). ’ Staff members =’ . array_sum($mystaff).’’;
Adding Array Elements
You could add a new employee quickly using the array_push() function.
array_push($mystaff[‘John’], 55000);
John would be added to the end of the array. Calling asort(), after array_push(), would alphabetize the array properly.
Deleting Array Elements
If John was hired to replace Matthew, the unset function would send Matthew to the unemployment line and asort() would restore the array’s sorted order.
unset($mystaff[‘Matthew’]);
asort($mystaff);
Tutorial Summary
These are just a few of the 75 functions PHP makes available for working with arrays. You can find the entire list, along with their syntax, by visiting PHP’s online Array Function Manual. If you would like to learn more about PHP and Arrays we offer a extremely comprehensive PHP video tutorial that offers indepth, step-by-step instructions.
Comments
Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!
You must be logged in to post a comment.