PHP
  Home arrow PHP arrow Page 5 - Array Manipulation With PHP4
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

Array Manipulation With PHP4
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2001-11-09

    Table of Contents:
  • Array Manipulation With PHP4
  • Having Your Cake
  • When Size Does Matter...
  • Push And Pull
  • Slice And Dice
  • Where Am I?
  • Sorting Things Out
  • Flipping Out

  • 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


    Array Manipulation With PHP4 - Slice And Dice


    (Page 5 of 8 )

    PHP allows you to extract a subsection of the array with the array_slice() function, in much the same way that the substr() function allows you to extract a section of a string. Here's what it looks like:

    array_slice(array, start, length)
    where "array" is an array variable, "start" is the index to begin slicing at, and "length" is the number of elements to return from "start".

    Here's an example:

    <? // create array $colours = array("red", "green", "blue", "yellow"); // returns the array ("green", "blue") $slice = array_slice($colours, 1, 2); ?>
    You can also use a negative index for the "start" position, to force PHP to begin counting from the right instead of the left.

    <? // create array $colours = array("red", "green", "blue", "yellow"); // returns the array ("green", "blue") $slice = array_slice($colours, -3, 2); ?>
    The array_splice() function allows you to splice one or more values into an existing array. Here's what it looks like:

    array_splice(array, start, length, replacement-values)
    where "array" is an array variable, "start" is the index to begin slicing at, "length" is the number of elements to return from "start", and "replacement-values" are the values to splice in.

    Here's an example:

    <? // create array $trio = array("huey", "dewey", "louie"); // section to be spliced in $splice = array("larry", "curly", "moe"); // $trio now contains ("huey", "larry", "curly", "moe") array_splice($trio, 1, 2, $splice); ?>

    More PHP Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway