|
PHP Arrays Tutorial - Part 2.
Title / Free Demo : Beginners PHP Training Videos Advanced PHP ArraysThis tutorial covers creating Arrays - Suitable for all versions of PHPDon’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 ArrayIn the previous tutorial we created these two arrays:$mystaff = array(0=>’Matthew’, $salaries=array("Matthew"=>65000, 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; 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 Syntaxforeach( $mystaff as $Name => $salary){ Name: Matthew, Salary: $65000 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. PHP Array FunctionsHere are examples of how PHP array-specific functions provide quick ways to enhance the power of arrays in your PHP programs. Counting Array ElementsCount() 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.<br />’; Sorting Array ElementsTo sort the names alphabetically before displaying them, use the asort() function.asort($mystaff); Array Element MathTo 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).’<br />’; Adding Array ElementsYou 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 ElementsIf 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’]); Tutorial SummaryThese 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. |