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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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.



     
     
    >>> More PHP Articles          >>> More By Apress Publishing
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT