PHP
  Home arrow PHP arrow Page 6 - Arrays
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Arrays
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 36
    2005-06-23

    Table of Contents:
  • Arrays
  • Outputting Arrays
  • Testing for an array
  • Locating Array Elements
  • Determining Array Size and Uniqueness
  • Other Useful Array Functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Arrays - Other Useful Array Functions


    (Page 6 of 6 )

    In this section I introduce a number of array functions that perhaps don’t easily fall into one of the prior sections, but are nonetheless quite useful.

    array_rand()

    mixed array_rand(array input_array [, int num_entries])

    The array_rand() function will return one or more keys found in input_array. If you omit the optional num_entries parameter, only one random value will be returned. If num_entries is set to greater than one, that many keys will be returned. An example follows:

    $states = array("Ohio" => "Columbus", "Iowa" => "Des Moines","Arizona" => "Phoenix");
    $randomStates = array_rand($states, 2);
    print_r($randomStates);

    This returns:

    ===========================================================
    Array ( [0] => Arizona [1] => Ohio ) ===========================================================

    array_chunk()

    array array_chunk(array input_array, int size [, boolean preserve_keys])

    The array_chunk() function breaks input_array into a multidimensional array consisting of several smaller arrays consisting of size elements. If the input array can’t be evenly divided by size, the last array will consist of fewer than size elements. Enabling the optional parameter preserve_keys will preserve each value’s corresponding key. Omitting or disabling this parameter results in numerical indexing starting from zero for each array. An example follows:

    $cards = array("jh","js","jd","jc","qh","qs","qd","qc",
                   
    "kh","ks","kd","kc","ah","as","ad","ac");
    // shuffle the cards
    shuffle($cards);
    // Use array_chunk() to divide the cards into four equal "hands"
    $hands = array_chunk($cards, 4);
    print_r($hands);

    This returns the following (your results will vary because of the shuffle):

    ===========================================================
    Array ( [0] => Array ( [0] => jc [1] => ks [2] => js [3] => qd )
               [1] => Array ( [0] => kh [1] => qh [2] => jd [3] => kd )
               [2] => Array ( [0] => jh [1] => kc [2] => ac [3] => as )
               [3] => Array ( [0] => ad [1] => ah [2] => qc [3] => qs ) )============================================================

    Summary

    Arrays play an indispensable role in programming, and are ubiquitous in every imaginable type of application, Web-based or not. The purpose of this chapter was to bring you up to speed regarding many of the PHP functions that will make your programming life much easier as you deal with these arrays.

    The next chapter focuses on yet another very important topic: object-oriented programming. This topic has a particularly special role in PHP 5, because the process has been entirely redesigned for this major release.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · The writer forgot something important. Not only does array_pop return the last...
       · The capital of Nebraska, used in the example, is Lincoln (not Omaha).
     

    Buy this book now. This article is excerpted from the book Beginning PHP 5 and MySQL: From Novice to Professional, by W. Jason Gilmore (Apress, 2004; ISBN: 1893115518). Check it out at your favorite bookstore today. Buy this book now.

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT