Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL

Home > PHP > Array Functionsprinter version

PHP Array Functions

PHP provides a number of library functions for working on arrays.

Counting the Elements in an array

The count() function returns the number of elements in an array. It will return zero if the array is empty or the array variable has not been set.

Example of count()
<?php
    $names = array ("Tom", "Dick", "Harry");

    // This will print out 3
    echo count ($names);
?>


Finding the Maximum or Minimum Values in an array

The max() and min() functions will return the maximum and minimum values in an array respectively.

Both can be used with integers or floats and will return the appropriate type. They can be used with strings but may not return the results you expect.

Example of max() and min()
<?php
    $numbers = array (40, 23, -10, 4);

    // This will print out Maximum 40 Minimum -10
    echo ('Maximum '.max($numbers).' Minimum '.min($numbers));
?>


Finding Values in arrays

PHP provides the two functions in_array() and array_search().

in_array()

This function returns true if an array contains a specific value.

Example of in_array()
<?php
    $names = array ("Tom", "Dick", "Harry");

    // This will print out Harry is present
    if (in_array ("Harry", $names));
        echo ("Harry is present");
?>


The function also allows a third parameter that causes a strict type check to be made.

in_array() With and Without Type Checking
<?php
    $numbers = array (10, 20, 30, 40, 50);

    // This will print out 40 is present although the types are different
    if (in_array ("40", $numbers));
        echo ("40 is present");

    // This won't print anything because the types are different
    if (in_array ("40", $numbers, true));
        echo ("40 is present");
?>


array_search()

The array_search() function works in the same way as the in_array() function except that if the value is found it returns the key of the matching value instead of a Boolean value true.

If the value isn't found then false is returned.

Note: If the function is used on a numbered array and it finds the value in the first element then it will return 0 and this will evaluate as false unless the is_identical operator is used.

Example of array_search()
<?php
    $numbers = array (10, 20, 30, 40, 50);

    /* This will find the first element of the array
     but will return 0 which will evaluate as false
     therefore 10 is present will not be printed out */
    if (in_array (10, $numbers));
        echo ("10 is present");

    // If is_identical is used it will work
    $index = in_array (10, $numbers);
    if ($index === false);
        echo ("10 is not present");
    else
        echo ("10 is present");
?>


Reversing arrays

The array_reverse() function allows you to create a new array from an existing array with all its elements in reverse order.

Example of array_reverse()
<?php
    $countup = array (1, 2, 3, 4, 5);

    // Print out the indexes and element values
    foreach ($countup as $index => $number)
        echo('['.$index.']'.$number.', ');

    // Which gives [0]1, [1]2, [2]3, [3]4, [4]5,

    // Now reverse the order of elements in the array
    $countdown = array_reverse ($countup);

    // Print out the indexes and element values
    foreach ($countdown as $index => $number)
        echo('['.$index.']'.$number.', ');

    // Which gives [0]5, [1]4, [2]3, [3]2, [4]1,
?>


The array_reverse() function accepts an optional additional Boolean argument called preserve_key which maintains the relationship between the elements and the index.

Example of array_reverse() with preserve_key
<?php
    $countup = array (1, 2, 3, 4, 5);

    // Reverse the order of elements in the array but preserve the key
    $countdown = array_reverse ($countup, true);

    // Print out the indexes and element values
    foreach ($countdown as $index => $number)
        echo('['.$index.']'.$number.', ');

    // Which gives [4]5, [3]4, [2]3, [1]2, [0]1,
?>


Previous - PHP arrays Previous - Arrays     Next - Sorting Arrays Next - PHP sorting arrays


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
PHPBasicsCreating a ScriptRunning a ScriptVariablesExpressions and OperatorsStringsStrings and SubstringsReplacing SubstringsRegular ExpressionsBranches and ConditionsLoopsArraysPHP array functionsArray FunctionsSorting ArraysUser-Defined FunctionsInclude and RequireUploading FilesFile FunctionsSession Variables
Recommended Reading
PHP and MySQL web development

programming PHP

PHP cookbook
Tips and TricksPage TemplatesForm Reply ScriptsForm ValidationJavaScript to PHP
Got any PHP Tips?
Send me your tip and if it's suitable I'll put it on the site, credit it to you and add a link back to your site.